Angular Route:
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
一切都按预期工作,但是如果我在浏览器中放置相同的网址(没有前导/#/
),部分仍然会返回,这是不好的。
如何防止这种情况发生?
答案 0 :(得分:0)
您可以将自定义ActionMethodSelectorAttribute用作服务器端方法。例如:
public class AjaxRequestAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext,
MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request.IsAjaxRequest();
}
}
并使用:
[AjaxRequest]
public JsonResult GetTemplates()
{
...
}