3

How can I create a custom route constraint that will only match AJAX requests?

In a controller I can use Request.IsAjaxRequest().

How can I access IsAjaxRequest from my custom constraint? I've implemented the IRouteConstraint interface and the Match() method as follows:

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?
    }
}

1 Answer 1

5

If you add this to your route constraint, does the httpContext.Request.IsAjaxRequest() work?

using System.Web.Mvc;

Request.IsAjaxRequest() is an extension method in System.Web.Mvc.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Knew I was overlooking something simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.