gpt4 book ai didi

c# - 具有 MVC5 属性路由的模糊路由

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

我在使用属性路由时遇到路由不明确的问题。问题源于(我相信)在我们的路线的根部使用可变参数。令我苦恼的是,文字参数似乎没有优先级,MVC5 无法确定使用哪个路由。

我之前在使用其他路由时遇到过这个问题,并认为我已经通过定义约定路由来解决了这个问题。考虑到这一点,我在哪里可以找到有关属性路由和解决歧义的最佳实践的更多信息?

这是我遇到问题的代码,以及异常。

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:

AccountController

RoundController

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: 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:

AccountController

RoundController

路由配置.cs

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

// I added this constraint resolver to resolve some other ambiguous routes that end
// with a literal, but MVC5 wasn't able to determine whether to use those routes
var constraintResolver = new System.Web.Mvc.Routing.DefaultInlineConstraintResolver();
constraintResolver.ConstraintMap.Add("notWriteAction", typeof(IsNotWriteActionConstraint));

routes.MapMvcAttributeRoutes(constraintResolver);

// This is the convention route I added to resolve another ambiguous route.
routes.MapRoute(
name: "Account",
url: "Account/{action}/{GroupName}/{AccessToken}",
defaults: new { controller = "Account", action = "Login", GroupName = UrlParameter.Optional, AccessToken = UrlParameter.Optional }
);

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

圆形 Controller .cs

public class RoundController : ControllerBase
{
[Route("{groupid}/{campaignid}/{accesstoken}")]
public async Task<ActionResult> TempRoundLink(string groupid, string campaignid, string accesstoken)
{
}
}

AccountController.cs

public class AccountController : ControllerBase
{
[AllowAnonymous]
[Route("Account/ResetPassword/{token}")]
public async Task<ActionResult> ResetPassword(string token)
{
}
}

最佳答案

我知道这不是你要问的,但我认为这可以通过使用 RoutePrefix 属性来解决。例如

[RoutePrefix("Round")]
public class RoundController : ControllerBase
{
[Route("{groupid}/{campaignid}/{accesstoken}")]
public async Task<ActionResult> TempRoundLink(string groupid, string campaignid, string accesstoken)
{
}
}

[RoutePrefix("Account")]
public class AccountController : ControllerBase
{
[AllowAnonymous]
[Route("Account/ResetPassword/{token}")]
public async Task<ActionResult> ResetPassword(string token)
{
}
}

我认为它试图不做出假设的原因是偏好可能并不总是可以确定的。例如。您希望路由器如何解决这两个问题:

Routes (from attributes):
"Account/ResetPassword/{token}"
"Account/{something}/alpha"

Reequest:
"/Account/ResetPassword/alpha"

但这只是一个猜测...

关于c# - 具有 MVC5 属性路由的模糊路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22336615/

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