[英]How to route multiple parameters in MVC and C#
我將默認 MVC 路由設置為:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
我想要做的是在我的搜索 controller 命中有以下路線。
.../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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.