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:
namespace System.Web.Mvc { public static class AjaxRequestExtensions { public static bool IsAjaxRequest(this HttpRequestBase request); } }
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?