【问题标题】:prevent users from getting partials防止用户获取部分内容
【发布时间】:2013-05-04 12:15:57
【问题描述】:

角度路线:

angular.module('AMS', []).
  config(['$routeProvider', function ($routeProvider) {
      $routeProvider.
          when('/dashboard', { templateUrl: '/Dashboard', controller: dashboardController }).
          when('/settings', { templateUrl: '/Settings', controller: settingsController }).
          otherwise({ redirectTo: '/dashboard' });
  }]);

示例:

/* client side route */
http://localhost:4117/#/dashboard

指向

/* controller that returns the partial */
http://localhost:4117/Dashboard

一切都按预期工作,但如果我将相同的 url 放入浏览器(没有前导 /#/),部分仍然会返回,这不好。

如何防止这种情况发生?

【问题讨论】:

  • Ghetto 装配方式:没有返回视图的索引操作,但这感觉不对,即使它有效。此外,它只能防止意外查看,因为任何人都可以转到路由配置并查看 ASP.NET MVC 路由。

标签: asp.net-mvc angularjs asp.net-mvc-routing


【解决方案1】:

您可以将其用作服务器端方法的自定义 ActionMethodSelectorAttribute。例如:

public class AjaxRequestAttribute : ActionMethodSelectorAttribute
{
    public override bool IsValidForRequest(ControllerContext controllerContext,
                                           MethodInfo methodInfo)
    {
        return controllerContext.HttpContext.Request.IsAjaxRequest();
    }
}

并使用:

[AjaxRequest]
public JsonResult GetTemplates()
{
...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 2013-05-05
    相关资源
    最近更新 更多