简体   繁体   English

请求匹配多个操作,导致ASP.NET 5 / MVC 6中具有不同参数的操作不明确

[英]Request matched multiple actions resulting in ambiguity for actions with different parameters in ASP.NET 5 / MVC 6

I have a simple route in my project: 我的项目中有一条简单的路线:

routes.MapRoute(
      name: "api",
      template: "api/{controller}/{action}");

In my controller I have two actions: 在我的控制器中,我有两个动作:

    [HttpGet]
    public string Get(string value)
    {
        return value;
    }

    [HttpGet]
    public string Get(int id)
    {
        return id.ToString();
    }

Now when I try to do a url like api/controller/get?id=1 it does not work because the framework cannot distinguish between two actions. 现在,当我尝试像api/controller/get?id=1这样的URL时,它不起作用,因为框架无法区分两个动作。 As far as I remember it did work pretty well in ordinary web api because it's obvious that this url matches only one of the actions based on it's parameter. 据我所知,它在普通的web api中运行得非常好,因为很明显这个url只根据它的参数匹配其中一个动作。 Did I do something wrong or it's not supported in the new MVC6? 我做错了什么或新的MVC6不支持?

Did I do something wrong or it's not supported in the new MVC6? 我做错了什么或新的MVC6不支持?

MVC Action Selector dosen't regard Action's parameters during select action. MVC操作选择器在选择操作期间不考虑Action的参数。 Therefore you can't have two actions correspond one route template. 因此,您不能有两个操作对应一个路径模板。 Except eg actions have different Action Constraints (HttpPost, HttpGet). 除了例如动作具有不同的动作约束(HttpPost,HttpGet)。

Choose action logic in code. 在代码中选择动作逻辑

In theory choosen logic between some actions based on parameters have to be into SelectBestActions method, but it do nothing 理论上,基于参数的某些动作之间选择逻辑必须是SelectBestActions方法,但它什么都不做

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

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