用于AJAX请求的ASP.NET MVC自定义路由约束

Mar*_*ark 3 asp.net-mvc

如何创建仅匹配AJAX请求的自定义路由约束?

在我可以使用的控制器中Request.IsAjaxRequest().

如何IsAjaxRequest从我的自定义约束中访问?我已经实现了如下IRouteConstraint界面和Match()方法:

public class IsAjaxConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext,
                      Route route, 
                      string parameterName,
                      RouteValueDictionary values,
                      RouteDirection routeDirection)
    {            
     // How can I check for an Ajax request here?
    }
}
Run Code Online (Sandbox Code Playgroud)

haw*_*kke 5

如果将其添加到路径约束中,httpContext.Request.IsAjaxRequest()是否有效?

using System.Web.Mvc;
Run Code Online (Sandbox Code Playgroud)

Request.IsAjaxRequest()是System.Web.Mvc中的扩展方法.