英文:
How to generate a URL with Attribute Routing based on the controller template and Action Template
问题
I have the following URL template in the IdeasController
:
[Controller]
[Route("gemeente/{municipalityName}/election/{electionId}/ideas/", Name = "Ideas")]
public class IdeasController : Controller
I also have this Action
with route template:
[Route("theme/{themeId}/", Name = "GetTheme")]
public IActionResult GetTheme(int themeId)
{
Console.WriteLine("Get theme----------------------------");
IEnumerable<Idea> ideas = _ideasManager.GetIdeasByTheme(themeId);
GeneralTheme theme = _themeManager.GetGeneralTheme(themeId);
IdeasByThemeDto ideasByThemeDto = new IdeasByThemeDto
{
Ideas = ideas,
Theme = theme
};
ViewBag["Title"] = "Ideas by theme: " + theme.Name;
return View("IdeasByTheme", ideasByThemeDto);
}
How can I generate a URL to arrive at gemeente/Dendermonde/election/1/ideas/theme/2
in the view?
Concrete example in view:
I have a collection of ideas, each idea has a theme with a themeId
.
I want to generate a URL that appends the current URL (/gemeente/{municipalityName}/election/{electionId}/ideas/
) with the theme id (theme/{themeId}
), which basically combines the two ("Ideas" and "GetTheme") templates together.
In the view:
@Url.RouteUrl("GetTheme22", new { themeId = Model.Theme.Id }) //empty string
Note: municipality name and election id should also be dynamically inserted, based on the previous request.
英文:
I have the following URL template in the IdeasController
:
[Controller]
[Route("/gemeente/{municipalityName}/election/{electionId}/ideas/"Name = "Ideas")]
public class IdeasController : Controller
I also have this Action
with route template:
[Route("theme/{themeId}/",Name = "GetTheme")]
public IActionResult GetTheme(int themeId)
{
Console.WriteLine("Get theme----------------------------");
IEnumerable<Idea> ideas = _ideasManager.GetIdeasByTheme(themeId);
GeneralTheme theme = _themeManager.GetGeneralTheme(themeId);
IdeasByThemeDto ideasByThemeDto = new IdeasByThemeDto
{
Ideas = ideas,
Theme = theme
};
ViewBag["Title"] = "Ideas by theme: " + theme.Name;
return View("IdeasByTheme", ideasByThemeDto);
}
How can I generate a URL to arrive at gemeente/Dendermonde/election/1/ideas/theme/2
in the view?
Concrete example in view:
I have a collection of ideas, each idea has a theme with a themeId
.
I want to generate a URL that appends the current URL (/gemeente/{municipalityName}/election/{electionId}/ideas/
) with the theme id (theme/{themeId}
), which basically combines the two ("Ideas" and "GetTheme") templates together.
In the view:
@Url.RouteUrl("GetTheme22", new { themeId = Model.Theme.Id }) //empty string
Note: municipality name and election id should also be dynamically inserted, based on the previous request.
答案1
得分: 1
根据所示的路由模板,您可以使用Url.RouteUrl
生成链接:
<a href="@Url.RouteUrl("GetTheme", new { municipalityName="Dendermonde", electionId=1, themeId=2 })">获取主题</a>
这将解析为:
<a href="gemeente/Dendermonde/election/1/ideas/theme/2">获取主题</a>
关于如何动态插入MunicipalitName
和electionId
的信息,可以根据个人偏好和特定用例的因素来决定。以下示例从ViewBag
中提取了控制器模板参数,假设它们是从上一个请求中存储在那里的:
<a href="@Url.RouteUrl("GetTheme", new {
municipalityName=ViewBag["MunicipalitName"],
electionId=ViewBag["electionId"],
themeId=Model.Theme.Id})">获取主题</a>
而以下示例假设一切都存储在模型中:
<a href="@Url.RouteUrl("GetTheme", new {
municipalityName=Model.MunicipalitName,
electionId=Model.ElectionId,
themeId=Model.Theme.Id})">获取主题</a>
参考:在ASP.NET Core中路由到控制器操作 - 通过路由生成URL
英文:
Given the shown route templates you could generate a link using Url.RouteUrl
:
<a href="@Url.RouteUrl("GetTheme", new {municipalityName="Dendermonde", electionId=1, themeId=2})">Get Themes</a>
which would resolve to
<a href="gemeente/Dendermonde/election/1/ideas/theme/2">Get Themes</a>
Reference Routing to controller actions in ASP.NET Core - Generate URLs by route
> how can MunicipalitName
and electionId
be dynamically inserted.
How that information is passed to the view is up to personal preference and factors specific to the use case.
Here, the following extracts the controller template parameters from the ViewBag
assuming it was stored there from the previous request
<a href="@Url.RouteUrl("GetTheme", new {
municipalityName=ViewBag["MunicipalitName"],
electionId=ViewBag["electionId"],
themeId=Model.Theme.Id})">Get Themes</a>
While the following assumes everything was stored in the Model
<a href="@Url.RouteUrl("GetTheme", new {
municipalityName=Model.MunicipalitName,
electionId=Model.ElectionId,
themeId=Model.Theme.Id})">Get Themes</a>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论