English 中文(简体)
改道航道和路面路边的行动增加了路面的铺设
原标题:RedirectToAction with routeValues and Route Template adds query string

我与一些控制人员一道,在座采用“后转”模式,我对<代码>Redirect ToAction有麻烦。 下面是一些例子,只是为了显示行为而调整的部分:

    public class TestController : Controller
    {
        [Route("{ID1}/Test")]
        [Route("{ID2}/{ID1}/Test")]
        public IActionResult Test(string id1, string id2)
        {
            return RedirectToAction("Redirected", new { ID1 = id1, ID2 = id2 });
        }

        [Route("{ID1}/Redirected")]
        [Route("{ID2}/{ID1}/Redirected")]
        public string Redirected()
        {
            return "Redirected";
        }
    }

如果我要求<代码>/a/test, Im 改为a/Redirected 同我一样,我期望/现在也一样。

如果我请求<代码>a/b/test, 页: 1 ID2=a,这不是我期望或想要的东西!

我预计将改用<代码>a/b/Redirected。

为什么第二个具体路线不匹配? (注:似乎未对具体指明的<代码>/代码>作出任何改动。)

我能做些什么来帮助/加强我期望的匹配?

问题回答

Try to use regular expression:

[Route(@"{id1:regex(.*$)}/{id2:regex(.*$)}/{action}")]
[Route(@"{id1:regex(.*$)}/{action}")]        
public IActionResult Test(string id1, string id2)
{
    return RedirectToAction("Redirected", routeValues:new { ID1 = id1, ID2 = id2 });
}

[Route(@"{id1:regex(.*$)}/{id2:regex(.*$)}/{action}")]
[Route(@"{id1:regex(.*$)}/{action}")]       
public string Redirected()
{
    var parameters = this.HttpContext.GetRouteData();
    return "Redirected";
}

Why can the second specified Route not be matched? (note, it seems to make no difference which order the Routes were specified)

我能做些什么来帮助/加强我期望的匹配?

Well, based on your scenario and description, the behavior you re observing is due to the way route matching works in ASP.NET Core. When you specify multiple route templates, the routing system tries to match incoming requests to those templates in the order they are declared.

根据你的代码,由于RedirectToAction试图根据你提供的路线价值对目标行动路线模板进行匹配,因此出现了意想不到的查询代码?

While you supply ID1 = b and ID2 = a, it first finds a match with the template {ID1}/Redirected, resulting in /b/Redirected. However, it then sees the additional ID2 value that doesn t fit this template, so it appends it as a query string.

为了实现你想要的行为,你需要更明确地说明使用哪条路线模板。 你们可以通过向每个路线模板提供具体路线名称来做到这一点,然后在重新定位时具体说明路线名称。

您可以认为:

public class TestController : Controller
{
    [Route("{ID1}/Test", Name = "TestRoute1")]
    [Route("{ID2}/{ID1}/Test", Name = "TestRoute2")]
    public IActionResult Test(string id1, string id2)
    {
        return RedirectToAction("Redirected", new { ID1 = id1, ID2 = id2 });
    }

    [Route("{ID1}/Redirected", Name = "RedirectedRoute1")]
    [Route("{ID2}/{ID1}/Redirected", Name = "RedirectedRoute2")]
    public string Redirected()
    {
        return "Redirected";
    }
}

现在,在重新定位时,明确指明路线名称如下:

return RedirectToAction("Redirected", "Test", new { ID1 = id1, ID2 = id2 }).WithRouteName("RedirectedRoute1");

In addition, you also could consider attribute routing.Because, it provides clearer and more fine-grained control over route mapping.

[HttpGet("{ID2}/{ID1}/Redirected")]
public string Redirected() { ... }

<>说明: <https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnet-8.0#conventional-routing-order”rel=“nofollow noretinger”>refered to this official document> for





相关问题
ASP.NET Core MVC Register/Login Pages not working

I am following Tim Corey s video on "ASP.NET Core MVC" and I ve created a test project with a register/login authentication system using Identity (in .NET 6.0). My issue is that the Register ...

Upload in the database and download it in ASP.NET Core V5

I want the file that I uploaded in the documentUploadProvinceLevels database table to be able to be download.I implemented the file upload part, but I don t know what to do for downloading. please ...

Set selected value to @html.dropdownlistfor control

My View code: Unit @Html.DropDownListFor(m => m.Unit, UnitList, new { @class = "form-select" }) Gatepass Category @Html.DropDownListFor(m => m.GatePassCategory, GatepassCategoryList, ...