You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于控制器与Action路由模板生成特性路由URL

解决方案

问题根源

  1. 你使用了错误的路由名称GetTheme22,但Action上定义的路由名是GetTheme
  2. 生成URL时,路由前缀要求的municipalityName和electionId参数可从当前请求上下文自动补全,无需手动硬编码

正确代码

直接使用正确的路由名,仅传递新增的themeId即可:

@Url.RouteUrl("GetTheme", new { themeId = Model.Theme.Id })

如果需要显式传递所有参数(更直观),可从当前请求的路由数据中获取已有参数值:

@Url.RouteUrl("GetTheme", new { 
    municipalityName = ViewContext.RouteData.Values["municipalityName"],
    electionId = ViewContext.RouteData.Values["electionId"],
    themeId = Model.Theme.Id 
})

原理说明

  • ASP.NET Core路由系统会自动从当前请求的RouteData中提取municipalityName和electionId的参数值,无需手动重复传递
  • 控制器的路由前缀/gemeente/{municipalityName}/election/{electionId}/ideas/与Action的路由theme/{themeId}/会自动拼接成完整模板,最终生成gemeente/Dendermonde/election/1/ideas/theme/2格式的目标URL

内容的提问来源于stack exchange,提问作者Mendi Sterenfeld

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:05:10