生成基于控制器模板和操作模板的属性路由的URL。

huangapple go评论60阅读模式
英文:

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(&quot;/gemeente/{municipalityName}/election/{electionId}/ideas/&quot;Name = &quot;Ideas&quot;)]

public class IdeasController : Controller

I also have this Action with route template:

[Route(&quot;theme/{themeId}/&quot;,Name = &quot;GetTheme&quot;)]
public IActionResult GetTheme(int themeId)
{
    Console.WriteLine(&quot;Get theme----------------------------&quot;);
    
    IEnumerable&lt;Idea&gt; ideas = _ideasManager.GetIdeasByTheme(themeId);
    GeneralTheme theme = _themeManager.GetGeneralTheme(themeId);
    IdeasByThemeDto ideasByThemeDto = new IdeasByThemeDto
    {
        Ideas = ideas,
        Theme = theme
    };
    ViewBag[&quot;Title&quot;] = &quot;Ideas by theme: &quot; + theme.Name;
    
    return View(&quot;IdeasByTheme&quot;, 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(&quot;GetTheme22&quot;, 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>

关于如何动态插入MunicipalitNameelectionId的信息,可以根据个人偏好和特定用例的因素来决定。以下示例从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:

&lt;a href=&quot;@Url.RouteUrl(&quot;GetTheme&quot;, new {municipalityName=&quot;Dendermonde&quot;, electionId=1, themeId=2})&quot;&gt;Get Themes&lt;/a&gt;

which would resolve to

&lt;a href=&quot;gemeente/Dendermonde/election/1/ideas/theme/2&quot;&gt;Get Themes&lt;/a&gt;

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

&lt;a href=&quot;@Url.RouteUrl(&quot;GetTheme&quot;, new {
    municipalityName=ViewBag[&quot;MunicipalitName&quot;], 
    electionId=ViewBag[&quot;electionId&quot;], 
    themeId=Model.Theme.Id})&quot;&gt;Get Themes&lt;/a&gt;

While the following assumes everything was stored in the Model

&lt;a href=&quot;@Url.RouteUrl(&quot;GetTheme&quot;, new {
    municipalityName=Model.MunicipalitName, 
    electionId=Model.ElectionId, 
    themeId=Model.Theme.Id})&quot;&gt;Get Themes&lt;/a&gt;

huangapple
  • 本文由 发表于 2023年4月11日 00:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定