c# - 如何在 MVC 和 C# 中路由多个参数
问题描述
我的默认 MVC 路由设置为:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我想要做的是在我的搜索控制器命中有以下路线。
.../Search/Uk
.../Search/Uk/County/Buckinghamshire
.../Search/Uk/City/London
.../Search/Uk/Town/Ashford
.../Search/Uk/Postcode/AB-Aberdeen
我只有一个名为“索引”的视图。据我了解路由,我认为我应该能够做这样的事情:
public ActionResult Index(string country)
public ActionResult Index(string country, string searchType, string location)
但是没有雪茄,任何人都明白我做错了什么,我需要添加某种路线配置吗?事实上,我什至无法加载搜索页面
解决方案
您可以使用基于属性的路由,您可以在路由本身中传递参数。
喜欢,
//I hope you have already enabled attribute routing and search controller with RoutePrefix as "search"
[Route("{country}")]
public ActionResult Index(string country)
{
//Your business logic
}
[Route("{country}/{searchType}/{location}")]
public ActionResult Index(string country, string searchType, string location)
{
//Your business logic
}
推荐阅读
- ruby - 重构口袋妖怪类型的周末
- python - 散点图 ValueError:x 和 y 的大小必须相同
- java - 在 when 子句中使用成员对象的字段创建 Drools
- javascript - Vue.js:在 HMR 上解决的不一致的“未知自定义元素”
- python - 如何使用 SimpleGraphics 的用户输入绘制形状?
- r - 当cols =以某个前缀开头的任何列时如何使用tidyr pivot_longer
- javascript - 如何在javascript中缓存帖子然后只获取新帖子?
- sql - 如何从日期时间字段中减去时间来调整日期时间字段?
- svelte - 影响 svelte 中 if/else 结果的组件
- firebase - 如何通过 REST 在 Firestore 文档中添加/删除数组元素?