Dynamically modify the action name in Asp.net web API

Source: Internet
Author: User
Tags jqgrid

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 }

 

 

Related Article

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.