題:
編輯: 看來我的問題實際上不是路由問題而是錨定問題。
如果我已經分配了一條路線:
[Route("~/Envelope/List/AcademicYear/{year}")]
public IActionResult AcademicYear(string year)
{
}
我將如何正確使用 asp-action 來呼叫這條路線?使用
<a asp-action="List/AcademicYear/" asp-route-id="@Model.AcademicYear">@Model.AcademicYear</a>
回傳一個帶有 / (Envelopes/List/AcademicYear/2122) 而不是 / (Envelopes/List/AcademicYear/2122) 的 url,從而導致 404 錯誤
如何使用帶有 asp-action 的自定義 URL 來呼叫控制器中的特定操作?
或者
如何更改路由以便我可以從具有非默認路由映射的控制器呼叫操作?
語境:
我已經閱讀了https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-5.0 但我仍然對路由的整個概念以及如何互動感到困惑帶有控制器和動作。
在我的應用程式中,我有一個名為 Envelope 的控制器——它控制著與我的 Envelopes 相關的一切。
我在信封控制器中有一個名為
public class EnvelopeController : Controller {
public IActionResult List() {... return View()}
它回傳 List.cshtml 視圖。默認路由映射設定的當前 url:/Envelope/List
在 List.cshtml 中,我有一個鏈接,旨在根據年份引數過濾串列
<a asp-action="AcademicYear" asp-route-academicYear="@Model.AcademicYear"> @Model.AcademicYear</a>
我的目的是將其傳遞到 Envelopes 控制器中名為“AcademicYear”的方法中,該方法收集存盤在臨時資料中的 Envelope 資料,對其進行反序列化,然后根據引數回傳過濾后的版本:
public IActionResult AcademicYear(string academicYear) { return View("List", newViewModel)}
此點之后的回傳 url 正確:/Envelope/AcademicYear?academicYear=21/22
但是我想知道如何改變這一點,所以即使我呼叫了 Action
<a asp-action="AcademicYear" asp-route-academicYear="@Model.AcademicYear"/>
回傳的 URL 看起來像這樣/Envelope/List/AcademicYear/2122/
Is there a way of doing this? Am I looking at the problem the wrong way? I have thought about simply passing a parameter in the List action and running some form of control to do either set of operations depending on the parameters existence but realistically the List method and the filtering AcademicYear method aren't really doing the same thing and I'd like to seperate out the code into different methods if possible.
Even if its not the appropriate solution I would still like to know if it is possible to change the URL routing for an action after it has been called.
Edit : I have tried using HttpGet(List/AcademicYear/{academicYear:[a-zA-Z]} however when I do this I can't actually call List/AcademicYear as an asp-action due to the "/" and how that encodes to /
Answer:
在以下解決方案的幫助下,我意識到我看錯了問題,實際上在創建正確的錨點時遇到了問題。
閱讀:https : //docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-5.0#url-generation-and-ambient-values-1
我意識到答案正盯著我的臉,我將它與答案中提供的路線一起使用
<a id="@academicYear.Name" href="@Url.Action("AcademicYear", "Envelope",new {year= academicYear.Name})">
uj5u.com熱心網友回復:
也許你只需要像這樣裝飾你的動作:
[HttpGet("List/AcademicYear/{year:int}")] // or HttpPost it depends on you
public IActionResult AcademicYear(string year) { }
uj5u.com熱心網友回復:
您可以向操作添加多個屬性路由。
[Route("~/Envelope/List/AcademicYear/{year}", Name="ListRoute")]
[Route("~/Envelope/AcademicYear/{year}")]
public IActionResult AcademicYear(string year) { }
如果你需要這個網址
http://localhost:xxxx/Envelope/List/AcademicYear/2021
您可以使用此 html 助手,它將使用屬性路由名稱為您創建正確的錨標記。
@Html.RouteLink(@Model.AcademicYear, "ListRoute", new { year = @Model.AcademicYear })
我在屬性路由中添加了 Name="ListRoute"(見上文)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/336104.html
