繁体   English   中英

带参数的Web API路由

[英]Web API routing with parameter

我想为我的WEBAPI设置路由,使用paramater的方法很少,没有参数的很少。 我想在控件中使用我的函数名在实际的url中用于操作。 为了解释它,这是我的路由映射:

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

以下是我添加的两个功能:

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

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

当我点击: root / api / controller name / FunctionWithoutParam时,它会调用相应的函数。 但我无法调用FunctionWithParam。 我试过了
root / api / controller name / FunctionWithParam / 10但这似乎不起作用。 我设置了一个断点,并且没有调用parmater的函数。 我做错了什么,我怎样才能让它发挥作用?

更改函数参数名称以匹配路径:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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