。Net Core MVC RedirectToRoutePermanent不会重定向到匹配的操作

问题描述 投票:0回答:1

我正在.Net核心mvc项目中,一项操作需要获取三个参数,所以我首先在Startup.cs

中创建了一条新路由
app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "partner_detail",
                    pattern: "Partners/Detail/{id}/{name}/{location}");

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}/{title?}");
            });

然后有一个表,用户可以单击特定的伙伴,它会调用partnercontroller

@Html.ActionLink(Model.Partner.Name, "Detail", "Partners", new { id = Model.Partner.Id, name = Model.Partner.Name, location = Model.Location }, new { @class = "anchor-1" })

我的操作方法如下,

[AllowAnonymous]
public IActionResult Detail(Guid id, string name, string location)
{
    string friendlyName = FriendlyUrlExtension.GetFriendlyTitle(name);
    string friendlyLocation = FriendlyUrlExtension.GetFriendlyTitle(location);

    if (!string.Equals(friendlyName, name, StringComparison.Ordinal) || !string.Equals(friendlyLocation, location, StringComparison.Ordinal))
    {
        return this.RedirectToRoutePermanent("partner_detail", new { id = id, name = friendlyName, location = friendlyLocation });
    }

    return View();
}

我正在将名称和位置转换为SEO友好名称,并将其重定向到上述路线,

格式化后的网址是

https://myhost:00001/Partners/Detail/571ee251-b342-4fd3-b3df-0471ed54078e/partner-1/location-7

我有两个要问的所有专家问题

  1. 当我使用锚标记进行操作时,它在查询字符串中传递路由值,有没有办法将它们作为路由参数传递?
  2. 为什么RedirectToRoutePermanent没有击中我的动作?

有人可以帮助我吗?

asp.net-mvc redirect routes asp.net-core-mvc asp.net-mvc-routing
1个回答
0
投票
好,我修好了它。我按如下方式使用.net核心锚标记帮助程序,

<a controller="Partners" asp-action="Detail" asp-route-id="@Model.Partner.Id" asp-route-name="@Model.Partner.Name" asp-route-location="@Model.Location">@Model.Partner.Name</a>

并从startup.cs中删除路由,并作为属性添加到控制器操作中。

[Route("Partners/Detail/{id}/{name}/{location}", Name = "partner_detail")]

并且任何使用锚标记帮助器的人都要确保您的路由参数名称与route属性中的参数相同。

关于锚标记帮助器MSDN

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.