Query string not working while using attribute routing

后端 未结 8 2314
無奈伤痛
無奈伤痛 2020-11-27 12:52

I\'m using System.Web.Http.RouteAttribute and System.Web.Http.RoutePrefixAttribute to enable cleaner URLs for my Web API 2 application. For most of

8条回答
  •  忘掉有多难
    2020-11-27 13:49

    Just a side note from my part as well. In order for queryString params to work, you need to provide a default value for your method parameters to make it optional. Just as you would also do when normally invoking a C# method.

    [RoutePrefix("api/v1/profile")]
    public class ProfileController : ApiController
    {
    
       ...
    
       [HttpGet]
       [Route("{profileUid}")]
       public IHttpActionResult GetProfile(string profileUid, long? someOtherId) 
       {
          // ...
       }
    
       ...
    
    }
    

    This allows me to call the endpoint like this:

    /api/v1/profile/someUid
    /api/v1/profile/someUid?someOtherId=123
    

提交回复
热议问题