gpt4 book ai didi

c# - 如何在 ASP.NET MVC 5 的两个不同 Controller 中访问具有相同名称的操作方法?

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:49 26 4
gpt4 key购买 nike

使用属性路由(Route Attribute),我在两个不同的 Controller 中有两个同名的 Action 方法如下

public class HomeController : Controller
{
//URL: /accessMethod
[Route(“accessMethod”)]
public ActionResult Method1()
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}

public class AccountController : Controller
{
//URL: /accessMethod
[Route(“accessMethod”)]
public ActionResult Method2()
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
}

同时点击 url http://localhost:1234/accessmethod , 出现如下错误

Server Error in '/' Application.

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

The request has found the following matching controller types: mvc_Test.Controllers.Page1Controller mvc_Test.Controllers.Page2Controller

请教各位,如何在不使用RoutePrefix 属性的情况下解决这个问题?我们可以使用 Controller 名称代替 RoutePrefix,对吗?那么属性路由的主要用途是什么?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com