ASP. NET MVC makes the Controller's Action accept only Ajax requests.

Source: Internet
Author: User

First, the AJAX request is essentially the same as the general Web request, both HTTP requests. Theoretically the server side is unable to distinguish the request is not AJAX request, but, since the title has been said, then there must be a way to do.

In the request message of the AJAX request, it often contains a piece: X-requested-with = XMLHttpRequest

That's how it's done on all the big JavaScript frameworks.

The server side can be based on this point to determine whether the request is an AJAX request.

in ASP. NET MVC, there is also an extension method:

1 namespaceSYSTEM.WEB.MVC2 {3     //Summary:4     //represents a class that extends the System.Web.HttpRequestBase class by adding the ability to determine whether an HTTP request is an AJAX request. 5      Public Static classajaxrequestextensions6     {7         //Summary:8         //determines whether the specified HTTP request is an AJAX request. 9         //Ten         //Parameters: One         //Request: A         //HTTP requests.  -         // -         //return Result: the         //true if the specified HTTP request is an AJAX request, otherwise false.  -         // -         //Exception: -         //System.ArgumentNullException: +         //The request parameter is null (Nothing in Visual Basic).  -          Public Static BOOLIsajaxrequest ( Thishttprequestbase request); +     } A}

However, this can only be called inside the action, making the action bloated and requiring that the result of the AJAX request be returned separately within the action.

It would be better if we could do it like HttpGet and HttpPost.

Looking at the data, it is necessary for us to write a Attribute, and this Attribute inherits from the abstract class Actionmethodselectorattribute.

Then it's good to run. The code is as follows:

1 namespaceSYSTEM.WEB.MVC2 {3     /// <summary>4     ///represents an attribute that is used to restrict the action method so that the method only processes AJAX requests. 5     /// </summary>6[AttributeUsage (AttributeTargets.Method, AllowMultiple =false, inherited =true)]7      Public Sealed classAjaxrequestattribute:actionmethodselectorattribute8     {9         /// <summary>Ten         ///Initializes a new instance of the Ajaxrequestattribute class.  One         /// </summary> A          PublicAjaxrequestattribute () -: This(true) -         { the         } -  -         /// <summary> -         ///Initializes a new instance of the Ajaxrequestattribute class.  +         /// </summary> -         /// <param name= "Isajaxrequest" >restricts whether the action method is an AJAX request. </param> +          PublicAjaxrequestattribute (BOOLisajaxrequest) A         { atIsajaxrequest =isajaxrequest; -         } -  -         /// <summary> -         ///indicates whether the action method is an AJAX request.  -         /// </summary> in          Public BOOLisajaxrequest -         { to             Get; +             Private Set; -         } the  *         /// <summary> $         ///determines how the action method AJAX request is valid for the specified controller context. Panax Notoginseng         /// </summary> -         /// <param name= "ControllerContext" >The controller context. </param> the         /// <param name= "MethodInfo" >information about how to do this. </param> +         /// <returns>true if the action method request is valid for the specified controller context; otherwise false. </returns> A[SuppressMessage ("Microsoft.Design","CA1062")] the          Public Override BOOLisvalidforrequest (ControllerContext controllercontext, System.Reflection.MethodInfo MethodInfo) +         { -             varIsajaxrequest =controllerContext.HttpContext.Request.IsAjaxRequest (); $             returnIsajaxrequest = =isajaxrequest; $         } -     } -}

PS: The actual use of the namespace does not recommend this (with the System class library or the generic third-party class library coincident), if one day the MVC team really add such a Attribute, then it will compile errors. This is just a demonstration, because you can't think of a good namespace at the moment. -_-|||

Then, as long as the Attribute Action is hit, the request must be an AJAX request if the constructor argument is true or if the parameterless constructor is called. If the constructor argument is false, it cannot be an AJAX request.

If the above conditions are not true, the 404 Not Found is returned.

ASP. NET MVC makes the Controller's Action accept only Ajax requests.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.