我的web api路由图正在返回多个动作

时间:2013-01-30 21:52:11

标签: asp.net-mvc asp.net-web-api

请求的网址:http://localhost/api/access/blob

路线:

config.Routes.MapHttpRoute(
    name: "AccessApi",
    routeTemplate: "api/{controller}/{action}"
);

定义的行动:

public class AccessController : ApiController
{
    // GET api/access/blob
    [HttpGet]
    public string Blob()
    {
        return "blob shared access signature";
    }

    // GET api/access/queue
    [HttpGet]
    public string Queue()
    {
        return "queue shared access signature";
    }
}

结果:

Multiple actions were found that match the request: 
    System.String Blob() on type Project.Controllers.AccessController 
    System.String Queue() on type Project.Controllers.AccessController

为什么不找到适当的行动?

1 个答案:

答案 0 :(得分:3)

您必须删除WebApiConfig.cs文件中的DefaultApi路由配置。只添加您的配置:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "AccessApi",
            routeTemplate: "api/{controller}/{action}"
            );
    }
}

它将与网址一起使用:

http://localhost/api/access/blob

http://localhost/api/access/queue