[英]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.