0

角度路线:

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 放在浏览器中(没有前导/#/),部分仍然会返回,这不好。

我该如何防止这种情况发生?

4

1 回答 1

0

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

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

并使用:

[AjaxRequest]
public JsonResult GetTemplates()
{
...
}
于 2013-05-26T18:47:45.977 回答