如何Area在ASP.NET Core中使用?
我有一个需要管理员部分的应用.此部分要求将其视图放置在该区域中.所有以请求开头的请求Admin/都需要重定向到该区域.
asp.net-mvc-routing asp.net-mvc-areas asp.net-core-mvc asp.net-core
我正在使用ASP.NET Core MVC创建一个网站.当我点击某个动作时,我收到此错误:
AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
Web.Controllers.ChangeEventsController.Create (Web)
Web.Controllers.ProductsController.CreateChangeEvent (Web)
Run Code Online (Sandbox Code Playgroud)
这就是我在我的ProductsController的index.cshtmlm中定义我的动作的方法:
<a asp-controller="ChangeEvents" asp-action="Create" asp-route-id="@item.Id">Create Change Event</a>
Run Code Online (Sandbox Code Playgroud)
这是我的路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
以下是我定义操作的方式:
// ChangeEventsController
[HttpGet("{id}")]
public IActionResult Create(Guid id)
// ProductsController
[HttpGet("{id}")]
public IActionResult CreateChangeEvent(Guid id)
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
更新
感谢@MegaTron的回复,但是我想知道为什么我不能为不同的控制器提供相同的操作路径.如果我有许多控制器,每个创建实体,我觉得你提出的解决方案不会很好地扩展.
我最近更新了Visual Studio forUpdate 3和ASP.Net Core for 1.0.0。
我按照文档中的教程进行操作,并尝试设置使用像这样的区域https://docs.asp.net/en/1.0.0/mvc/controllers/areas.html
但是,生成的链接是http://localhost:2187/?area=Admin,而不是http://localhost:2187/Admin/Home/Index
更新
我的路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "areaRoute",
template: "{area}/{controller=Home}/{action=Index}");
});
Run Code Online (Sandbox Code Playgroud)
怎么了?
解决方案
问题在于答案中提到的路线顺序。