且构网

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

使用不同控制器但动作名称相同的路由无法生成所需的网址

更新时间:2023-11-26 13:57:58

两个路由都具有相同的名称,这在ASP.NET Core MVC中不起作用.

Both your routes are named the same, this cannot work in ASP.NET Core MVC.

我不是在谈论方法命名,而是在谈论路由命名.您在 HttpPost 属性中使用相同的标识符 Name ="delete" 调用了两条路由.MVC中的路由名称唯一地标识了路由模板.

I'm not talking about the methods naming, but about routes naming. You called both your routes with the same identifier Name = "delete" inside the HttpPost attribute. Route names in MVC uniquely identifies a route template.

据我所知,您实际上不需要标识您的路由,而只是要区分不同的URI.因此,您可以在操作方法上***删除 HttpPost 属性的 Name 属性.这足以使ASP.NET Core路由器匹配您的操作方法.

From what I can see you do not really need to identify your routes, but only to distinguish different URIs. For this reason you may freely remove the Name property of HttpPost attribute on your action methods. This should be enough for ASP.NET Core router to match your action methods.

如果您只使用属性路由来还原内容,则***将控制器更改为以下内容:

If you, instead, what to revert using only attribute routing you better change your controller to the following:

// other code omitted for clarity
[Route("aim/v1/contacts/")]
public class aimContactsController : Controller
{
    [HttpPost("delete/{id}")]
    public IActionResult delete(string id)
    {
        // omitted ...
    }
}