Method with two parameters in asp.net web api

后端 未结 3 773
故里飘歌
故里飘歌 2021-02-02 10:02

How can I make a method with two parameters using ASP.NET Web Api?

So that I can call it like localhost/controller/param1/param2

相关标签:
3条回答
  • 2021-02-02 10:14

    I think the easiest way is to simply use AttributeRouting.

    [Route("api/YOURCONTROLLER/{paramOne}/{paramTwo}")]
    public string Get(int paramOne, int paramTwo) {
        return "The [Route] with multiple params worked";
    }
    

    The {} names need to match your parameters.

    Attribute Routing in ASP.NET Web API 2

    0 讨论(0)
  • 2021-02-02 10:25

    You can also call the url with specific params names in the querystring:

    /api/actions?param1=5&param2=1/1/2000
    

    Then the controller method would be:

    GetByParams(int param1, DateTime param2)
    
    0 讨论(0)
  • 2021-02-02 10:30

    Just change or add route in global.asax

    routes.MapHttpRoute(name: "DefaultApi1", routeTemplate: "api/{controller}/{id}/{name}", Defaults: new{} );
    
    0 讨论(0)
提交回复
热议问题