'The parameters dictionary contains a null entry' error, Web API

戏子无情 提交于 2019-12-04 09:28:15

Your second function has a parameter name, and the default parameter is called id. With your current setup, you can access the second function on

http://localhost:1895/API/Document/?name=test

To make the URLs work as you have specified previously, I would suggest using attribute routing and route constraints.

Enable attribute routing:

config.MapHttpAttributeRoutes();

Define routes on your methods:

[RoutePrefix("api/Document")]
public class DocumentController : ApiController
{    
    [Route("{id:int}")]
    [HttpGet]
    public String GetById(int id)   // The name of this function can be anything now
    {
        return "test";
    }

    [Route("{name}"]
    [HttpGet]
    public String GetByName(string name)
    {
        return "test2";
    }
}

In this example, GetById has a constraint on the route ({id:int}) which specifies that the parameter must be an integer. GetByName has no such constraint so should match when the parameter is NOT an integer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!