简体   繁体   English

Asp.Net Core 端点路由

[英]Asp.Net Core Endpoint routing

I have an Action in a Controller :我在Controller中有一个Action

[Route("")]
[Route("{pageNumber?}")]
[Route("tag/{tagSlug?}/{pageNumber?}")]
public async Task<IActionResult> Index(int? pageNumber, string tagSlug = null, string searchTerm = null)
{
    var model = await repoPm_.GetPage(pageNumber, tagSlug, searchTerm);
    return View(model);
}

Each item has a list of tags associated with it每个项目都有一个与之关联的标签列表

@foreach (var t in item.Tags)
{
    <a asp-action="Index" asp-route-tagSlug="@t.TagSlug" class="mr-3">@t.Name</a>
}

And in my navbar i have a link to the page, which, I've tried the following:在我的导航栏中,我有一个指向该页面的链接,我尝试了以下操作:

<a asp-area="" asp-controller="Items" asp-action="Index" asp-route-tagSlug="">Items</a>
<a asp-area="" asp-controller="Items" asp-action="Index" asp-route-tagSlug="@null">Items</a>
<a asp-area="" asp-controller="Items" asp-action="Index">Items</a>

When I'm not on that page, the link in the navbar points to /items/tag which is annoying, but it works.当我不在该页面上时,导航栏中的链接指向/items/tag这很烦人,但它可以工作。

When I am on that page and I've clicked on one of the tags then the Url in the Omnibar is proper, ie /items/tag/fancy-items当我在该页面上并单击其中一个标签时,Omnibar 中的 Url 是正确的,即/items/tag/fancy-items

The problem is that is also the generated url for the link in my navbar, ie /items/tag/fancy-items when it should be just /items问题是这也是为我的导航栏中的链接生成的 url,即/items/tag/fancy-items当它应该只是/items

What am i doing wrong?我究竟做错了什么? I don't want to get into a bunch of route-mapping in Startup.cs , seems like <a asp-area="" asp-controller="Items" asp-action="Index">Items</a> should just work我不想进入Startup.cs中的一堆路由映射,似乎<a asp-area="" asp-controller="Items" asp-action="Index">Items</a>应该只是工作

You can use overloads to separate your different routes such that their URLs are discrete from one another.您可以使用重载来分隔不同的路由,以使它们的 URL 彼此离散。

This way, calling your Index action with no parameters can only ever be reached by /items , and the run-time binding behavior of ASP.NET will not incorrectly resolve to a different URL.这样,只有/items才能调用不带参数的Index操作,并且 ASP.NET 的运行时绑定行为不会错误地解析为不同的 URL。

My suggestion would be to make the top-level Action calls overloads that resolve to the same underlying method for reusability.我的建议是使顶级 Action 调用重载,以解决相同的底层方法以实现可重用性。

[Route("")]
public async Task<IActionResult> Index()
{
    return await WrappedMethod(null);
}


[Route("tag/{pageNumber}")]
public async Task<IActionResult> Index(int pageNumber)
{
    return await WrappedMethod(pageNumber);
}

[Route("tag/{tagSlug}/{pageNumber}")]
public async Task<IActionResult> Index(string tagSlug, int? pageNumber)
{
    return await WrappedMethod(pageNumber, tagSlug);
}

private async Task<IActionResult> WrappedMethod(int? pageNumber, string tagSlug = null, string searchTerm = null)
{
    var model = await repoPm_.GetPage(pageNumber, tagSlug, searchTerm);
    return View(model);
}

Note that the nullability of the parameters themselves will depend on your use-cases.请注意,参数本身的可空性将取决于您的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AuthorizeAttribute 不适用于 ASP.NET Core 3.1 中的端点路由 - AuthorizeAttribute not working with Endpoint Routing in ASP.NET Core 3.1 ASP.NET Core 3 MVC端点路由和按路由定位 - ASP.NET Core 3 MVC endpoint routing and localization by route ASP.NET Core 2.2中的端点路由不起作用 - Endpoint Routing in ASP.NET Core 2.2 is Not Working 视图中的ASP.NET Core端点路由链接生成 - ASP.NET Core Endpoint Routing Link Generation in Views ASP.NET Web API (.NET Core 3.1) 中的端点路由问题 - Problem with endpoint routing in ASP.NET Web API (.NET Core 3.1) ASP.NET Core 3.0 端点路由不适用于默认路由 - ASP.NET Core 3.0 Endpoint Routing doesn't work for default route 在 ASP.Net Core 2.2 MVC 中使用 Endpoint-Routing 时如何正确覆盖 IUrlHelper? - How top correctly override the IUrlHelper while using Endpoint-Routing in ASP.Net Core 2.2 MVC? 在具有端点路由的 ASP.NET 核心中使用 CreatedAtRouteResult 生成带有分段的 URL - Generate a URL with segments using CreatedAtRouteResult in ASP.NET Core with endpoint routing ASP.NET Core 3.0 使用属性路由更改默认端点路由 - ASP.NET Core 3.0 change default endpoint route with attribute routing ASP.NET Core 3.0 Endpoint Routing 不工作并且找不到 404 - ASP.NET Core 3.0 Endpoint Routing not working and getting 404 not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM