简体   繁体   中英

Web API routing with parameter

I want to setup routing for my WEBAPI to have few methods with paramater and few without parameters. I want to use my function names in the controller in the actual urls to be used for actions. To explain it more, here is my routing mapping:

            config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }

Here are the two functions I add:

    [HttpGet]
    public List<Category> FunctionWithParam(long param)
    {
        //return something
    }

    [HttpGet]
    public List<Category> FunctionWithoutParam()
    {
       //return something
    }

When I hit: root/api/controller name/FunctionWithoutParam it does call the appropriate function. But I cannot call FunctionWithParam. I tried
root/api/controller name/FunctionWithParam/10 But this does not seem to work. I put a breakpoint and the function with parmater is just not called. What am I doing wrong, How can I get this to work?

Change your function parameter name to match the route:

[HttpGet]
    public List<Category> FunctionWithParam(long id)
    {
        //return something
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM