In route settings, my route settings are as follows:
/API/{Controller}/Jqgrid/{Action}/{ID}
For the following URL, the list method of the usercontroller class is executed by default:
/API/User/Jqgrid/List
I want all routes whose URLs contain jqgrid to execute the "jqgrid _ {action}" method, that is, the jqgrid_list method. After a few days of suffering, finally solved. UpperCode(Here I copied my questions on stackoverflow and my own answers. You are welcome to point out the unauthentic English in this article. Thank you ):
first of all, I need to add a jqgridcontrollerconfiguration attribute to replace the default action selector applied to the Controller with my one.
[Jqgridcontrollerconfiguration]Public ClassUsercontroller: apicontroller {//Get:/API/user/jqgrid/List[Httpget]PublicJqgridmodel <user>Jqgrid_list () {jqgridmodel<User> result =NewJqgridmodel <user>(); Result. Rows=Get ();ReturnResult ;}}
Here's the code of jqgridcontrollerconfiguration:
1 Public Class Jqgridcontrollerconfiguration: attribute, icontrollerconfiguration 2 { 3 Public Void Initialize (httpcontrollersettings controllersettings, httpcontrollerdescriptor controllerdescriptor) 4 { 5 Controllersettings. Services. Replace ( Typeof (Ihttpactionselector ), New Jqgridactionselector ()); 6 } 7 }
In jqgridactionselector, the "Action" is modified if a "jqgrid/" exists in the request URL.
1 Public Class Jqgridactionselector: apicontrolleractionselector 2 { 3 Public Override Httpactiondescriptor selectaction (httpcontrollercontext controllercontext) 4 { 5 Uri url = Controllercontext. Request. requesturi; 6 If (URL. segments. Any (S => String . Compare (S, " Jqgrid/ " , True ) = 0 )) 7 { 8 Controllercontext. routedata. Values [ " Action " ] = " Jqgrid _ " + Controllercontext. routedata. Values [ " Action " ]. Tostring (); 9 } 10 11 Return Base . Selectaction (controllercontext ); 12 } 13 }