ASP.NET MVC the Controller of Action only accept an Ajax request. ASP.NET MVC the Controller of Action only accept an Ajax request.

ASP.NET MVC the Controller of Action only accept an Ajax request.

 

First, ajax request with the average web request is essentially the same, all http requests. Theoretically server is unable to distinguish between the time the request is not ajax request, but since the title has been said, it is definitely a way to do.

In the request packet's ajax request, often contain such a: X-Requested-With = XMLHttpRequest

This is also the major javascript framework to do so.

And the server can be determined according to this point, the times request is ajax request.

In ASP.NET MVC, the method also has an extension:

Copy the code
The System.Web.Mvc namespace. 1 
 2 { 
 . 3 // Summary: 
 4 // represents a class based on System.Web.HttpRequestBase is extended, in which the added function is determined whether the HTTP request AJAX request. 
 Public static class AjaxRequestExtensions. 5 
 . 6 { 
 . 7 // Abstract: 
 8 // HTTP request to determine whether the specified AJAX request. 
 9 // 
10 // Parameters: 
. 11 @ Request: 
12 is the HTTP request //. 
13 // 
14 // Returns: 
15 // if the specified HTTP request is an AJAX request, then true; otherwise false. 
16 // 
17 // anomaly: 
18 // System.ArgumentNullException: 
19 // Request parameter is null (in Visual Basic for Nothing). 
20 is static public BOOL IsAjaxRequest (the this HttpRequestBase Request);
21     }
22 }
Copy the code

But then, this can only be called internally Action, making the Action become bloated, need to return the internally Action whether as a result of ajax request.

If, as marked HttpGet, way HttpPost label like to do better.

Access to information obtained, to do so, we need to write one Attribute, and this Attribute inherited from the abstract class ActionMethodSelectorAttribute.

Then it is easier to handle. code show as below:

Copy the code
The System.Web.Mvc namespace. 1 
 2 { 
 . 3 /// <Summary> 
 . 4 /// denotes a characteristic, the characteristic for limiting the operation method, which is only to process the AJAX request. 
 ///. 5 </ Summary> 
 . 6 [the AttributeUsage (AttributeTargets.Method, the AllowMultiple = to false, Inherited = to true)] 
 . 7 Sealed public class AjaxRequestAttribute: ActionMethodSelectorAttribute 
 . 8 { 
 . 9 /// <Summary> 
new class 10 /// initialization AjaxRequestAttribute instance. 
///. 11 </ Summary> 
12 is public AjaxRequestAttribute () 
13 is: the this (to true) 
14 { 
15} 
16 
. 17 /// <Summary> 
new instance of the class 18 /// AjaxRequestAttribute initialization. 
19 /// </ summary>
20 /// <param name = "isAjaxRequest "> limit operation method whether AJAX request. </ param> 
21 is public AjaxRequestAttribute (BOOL IsAjaxRequest) 
22 is { 
23 is IsAjaxRequest = IsAjaxRequest; 
24} 
25 
26 is /// <Summary> 
27 /// operation method indicating whether the AJAX request. 
/// 28 </ Summary> 
29 IsAjaxRequest public BOOL 
30 { 
31 is GET; 
32 Private SET; 
33 is} 
34 is 
35 /// <Summary> 
36 /// AJAX request context determination operation method is valid for the designated controller. 
/// 37 [</ Summary> 
38 is /// <param name = "ControllerContext"> controller context. </ param>
39 /// <param name = "methodInfo "> information about the operation method. </ param> 
40 /// <Returns> If the operation control method of a request for a specified context valid, true; otherwise false. </ Returns> 
41 is the override public BOOL IsValidForRequest (ControllerContext the ControllerContext, System.Reflection.MethodInfo MethodInfo) 
42 is { 
43 is IF (ControllerContext == null) 
44 is { 
45 the throw ArgumentNullException The new new ( "ControllerContext"); 
46 is} 
47 
48 = BOOL IsAjaxRequest ControllerContext .HttpContext.Request.IsAjaxRequest (); 
49 == return IsAjaxRequest IsAjaxRequest; 
50} 
51 is} 
52 is}
Copy the code

PS: the practical application of the namespace is not recommended to do so (with the system libraries or generic third-party libraries coincidence), if one day plus the MVC team really such a Attribute, it will compile a mistake. Here for demonstration only, I can not think of a good name because of a temporary space. -_- |||

So, as long as the Attribute of Action marked, if the constructor argument is true or call the no-argument constructor, then the request must AJAX request. If the constructor argument is false, it is not AJAX request.

If the above condition is not satisfied, 404 Not Found is returned.

First, ajax request with the average web request is essentially the same, all http requests. Theoretically server is unable to distinguish between the time the request is not ajax request, but since the title has been said, it is definitely a way to do.

In the request packet's ajax request, often contain such a: X-Requested-With = XMLHttpRequest

This is also the major javascript framework to do so.

And the server can be determined according to this point, the times request is ajax request.

In ASP.NET MVC, the method also has an extension:

Copy the code
The System.Web.Mvc namespace. 1 
 2 { 
 . 3 // Summary: 
 4 // represents a class based on System.Web.HttpRequestBase is extended, in which the added function is determined whether the HTTP request AJAX request. 
 Public static class AjaxRequestExtensions. 5 
 . 6 { 
 . 7 // Abstract: 
 8 // HTTP request to determine whether the specified AJAX request. 
 9 // 
10 // Parameters: 
. 11 @ Request: 
12 is the HTTP request //. 
13 // 
14 // Returns: 
15 // if the specified HTTP request is an AJAX request, then true; otherwise false. 
16 // 
17 // exception: 
18 is a System.ArgumentNullException @:
19 // Request parameter is null (in Visual Basic for Nothing). 
20 is static public BOOL IsAjaxRequest (the this HttpRequestBase Request);
21     }
22 }
Copy the code

But then, this can only be called internally Action, making the Action become bloated, need to return the internally Action whether as a result of ajax request.

If, as marked HttpGet, way HttpPost label like to do better.

Access to information obtained, to do so, we need to write one Attribute, and this Attribute inherited from the abstract class ActionMethodSelectorAttribute.

Then it is easier to handle. code show as below:

Copy the code
The System.Web.Mvc namespace. 1 
 2 { 
 . 3 /// <Summary> 
 . 4 /// denotes a characteristic, the characteristic for limiting the operation method, which is only to process the AJAX request. 
 ///. 5 </ Summary> 
 . 6 [the AttributeUsage (AttributeTargets.Method, the AllowMultiple = to false, Inherited = to true)] 
 . 7 Sealed public class AjaxRequestAttribute: ActionMethodSelectorAttribute 
 . 8 { 
 . 9 /// <Summary> 
new class 10 /// initialization AjaxRequestAttribute instance. 
///. 11 </ Summary> 
12 is public AjaxRequestAttribute () 
13 is: the this (to true) 
14 { 
15} 
16 
. 17 /// <Summary> 
new instance of the class 18 /// AjaxRequestAttribute initialization. 
19 /// </ summary>
20 /// <param name = "isAjaxRequest "> limit operation method whether AJAX request. </ param> 
21 is public AjaxRequestAttribute (BOOL IsAjaxRequest) 
22 is { 
23 is IsAjaxRequest = IsAjaxRequest; 
24} 
25 
26 is /// <Summary> 
27 /// operation method indicating whether the AJAX request. 
/// 28 </ Summary> 
29 IsAjaxRequest public BOOL 
30 { 
31 is GET; 
32 Private SET; 
33 is} 
34 is 
35 /// <Summary> 
36 /// AJAX request context determination operation method is valid for the designated controller. 
/// 37 [</ Summary> 
38 is /// <param name = "ControllerContext"> controller context. </ param>
39 /// <param name = "methodInfo "> information about the operation method. </ param> 
40 /// <Returns> If the operation control method of a request for a specified context valid, true; otherwise false. </ Returns> 
41 is the override public BOOL IsValidForRequest (ControllerContext the ControllerContext, System.Reflection.MethodInfo MethodInfo) 
42 is { 
43 is IF (ControllerContext == null) 
44 is { 
45 the throw ArgumentNullException The new new ( "ControllerContext"); 
46 is} 
47 
48 = BOOL IsAjaxRequest ControllerContext .HttpContext.Request.IsAjaxRequest (); 
49 == return IsAjaxRequest IsAjaxRequest; 
50} 
51 is} 
52 is}
Copy the code

PS: the practical application of the namespace is not recommended to do so (with the system libraries or generic third-party libraries coincidence), if one day plus the MVC team really such a Attribute, it will compile a mistake. Here for demonstration only, I can not think of a good name because of a temporary space. -_- |||

So, as long as the Attribute of Action marked, if the constructor argument is true or call the no-argument constructor, then the request must AJAX request. If the constructor argument is false, it is not AJAX request.

If the above condition is not satisfied, 404 Not Found is returned.

Guess you like

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