gpt4 book ai didi

c# - 属性路由和RouteLink

转载 作者:太空宇宙 更新时间:2023-11-03 10:31:52 24 4
gpt4 key购买 nike

我将我的主视图定义为

<body>
<div>
<h1>Home</h1>
@Html.RouteLink("R1", "first", new { user="R1"})
@Html.RouteLink("R2", "second", new { company = "R2" })
</div>
</body>

登录 Controller 就像

public class LoginController : Controller
{
// GET: Login

[Route("{user}", Name = "first")]
public ActionResult Index(string user)
{
return View();
}
[Route("{company}", Name = "second")]
public ActionResult Index2(string company)
{
return View();
}
}

路由配置

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);


}
}

我想象在首页 R1R2 上单击它分别路由到正确的操作结果 Index 和 Index2。

但它会产生以下错误

The current request is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index(System.String) on type MVCTest.Controllers.LoginController System.Web.Mvc.ActionResult Index2(System.String) on type MVCTest.Controllers.LoginController

我不知道为什么会这样。

最佳答案

不要使用{user}{company},而是使用usercompany

例子:

    [Route("user", Name = "first")]
public ActionResult Index(string user)
{
return View();
}
[Route("company", Name = "second")]
public ActionResult Index2(string company)
{
return View();
}

关于c# - 属性路由和RouteLink,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29816707/

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