且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Web API“参数字典包含空条目"错误

更新时间:2023-01-13 15:26:28

第二个函数的参数为​​name,默认参数为id.使用当前设置,您可以访问第二个功能

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

要使URL按您之前指定的方式工作,我建议使用路由约束.

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

启用属性路由:

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";
    }
}

在此示例中,GetById在路由({id:int})上具有约束,该约束指定参数必须为整数. GetByName没有这样的约束,因此当参数不是整数时应该匹配.

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.