Web API routing with multiple parameters

后端 未结 2 1664
暖寄归人
暖寄归人 2021-01-31 16:45

I\'m trying to work out how to do the routing for the following Web API controller:

public class MyController : ApiController
{
    // POST api/MyController/GetA         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 16:59

    I have seen the WebApiConfig get "out of control" with hundreds of routes placed in it.

    Instead I personally prefer Attribute Routing

    You are making it confusing with POST and GET

    [HttpPost]
    public List GetAllRows(string userName, string tableName)
    {
       ...
    }
    

    HttpPost AND GetAllRows ?

    Why not instead do this:

    [Route("GetAllRows/{user}/{table}")]
    public List GetAllRows(string userName, string tableName)
    {
       ...
    }
    

    OR change to Route("PostAllRows" and PostRows I think that you are truly doing a GET request so the code I show should work for you. Your call from client would be WHATEVER is in the ROUTE , so it WILL FIND your METHOD with GetAllRows, but the method itself , that name CAN BE anything that you want, so as long as the caller matches the URL in ROUTE, you could put in GetMyStuff for the method if you really wanted to.

    Update:

    I actually prefer to be explicit with type of HTTP methods AND I prefer to match the route params to the method params

    [HttpPost]
    [Route("api/lead/{vendorNumber}/{recordLocator}")]
    public IHttpActionResult GetLead(string vendorNumber, string recordLocator)
    { .... }
    

    (the route lead does not need to match method name GetLead but you will want to keep the same names on the route params and the method params, even though you can change the order e.g. put recordLocator before vendorNumber even if the route is the opposite - I don't do that , as why make it more confusing to look at).

    Bonus: Now you can always use regex in routes as well, example

    [Route("api/utilities/{vendorId:int}/{utilityType:regex(^(?i)(Gas)|(Electric)$)}/{accountType:regex(^(?i)(Residential)|(Business)$)}")]
    public IHttpActionResult GetUtilityList(int vendorId, string utilityType, string accountType)
        {
    

提交回复
热议问题