How to determine whether in the Global.asax ajax request

Today, in an application scenario requires a request in Global.asax in determining whether the ajax request, but in ASP.NET MVC has provided a ready-made extension method IsAjaxRequest:

Copy the code
namespace System.Web.Mvc
{
    public static class AjaxRequestExtensions
    {
        public static bool IsAjaxRequest(this HttpRequestBase request);
    }
}
Copy the code

But this extension method only for HttpRequestBase, in Global.asax only HttpRequest, no HttpRequestBase.

Later found can help HttpRequestWrapper easily solve this problem, the sample code as follows:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    CNBlogs.Infrastructure.Logging.Logger.Default.Info("Is a ajax Request", 
        (new HttpRequestWrapper(Request)).IsAjaxRequest() + "\n" + Request.Url.AbsoluteUri);
}

The practical verification, the method is simple and effective!

[References]

How do I convert an HttpRequest into an HttpRequestBase object?

Guess you like

Origin www.cnblogs.com/Jeely/p/10951269.html