具有动态值的 MVC 路由
MVC routes with dynamic values
我正在为 MVC 路由而苦苦挣扎
我想获取以下网址;
- 示例。com/sector/
- 示例。com/sector/ddmmyyy/
- 示例。com/sector/ddmmyyy/article-with-seo-url/
所有 3 个值都是动态的(扇区/日期/SEOURL)。
我可以得到第一名(/sectors/)与:
routes.MapRoute(
"sectors",
"{seourl}",
new { controller = "NewsArticle", action = "sectors" }
);
但是我不确定如何获取其他 2 个动态值的路由。
谢谢
汤米
您可能需要为 Date
和 SEOURL
指定默认值:
routes.MapRoute(
"sectors",
"{sectorName}/{date}/{seourl}",
new { constroller = "NewsArticle", action = "sectors",
date = 0, seourl = string.Empty },
new { date = "\d+" });
如果你去http://www.example.com/apple
,那么:
- sectorName = apple
- 日期 = 0
- seourl = """(空字符串)
如果你去http://www.example.com/apple/240514
,那么:
- sectorName = apple
- 日期=240514
- seourl = """(空字符串)
您的 sectors
操作应该具有以下签名:
public ActionResult sectors(string sectorName, int date, string seourl)
并且(以防万一),我也会为操作的参数分配默认值:
public ActionResult sectors(string sectorName, int date = 0, string seourl = string.Empty);
我正在为 MVC 路由而苦苦挣扎 我想获取以下网址;
- 示例。com/sector/
- 示例。com/sector/ddmmyyy/
- 示例。com/sector/ddmmyyy/article-with-seo-url/
所有 3 个值都是动态的(扇区/日期/SEOURL)。
我可以得到第一名(/sectors/)与:
routes.MapRoute(
"sectors",
"{seourl}",
new { controller = "NewsArticle", action = "sectors" }
);
但是我不确定如何获取其他 2 个动态值的路由。
谢谢
汤米
您可能需要为 Date
和 SEOURL
指定默认值:
routes.MapRoute(
"sectors",
"{sectorName}/{date}/{seourl}",
new { constroller = "NewsArticle", action = "sectors",
date = 0, seourl = string.Empty },
new { date = "\d+" });
如果你去http://www.example.com/apple
,那么:
- sectorName = apple
- 日期 = 0
- seourl = """(空字符串)
如果你去http://www.example.com/apple/240514
,那么:
- sectorName = apple
- 日期=240514
- seourl = """(空字符串)
您的 sectors
操作应该具有以下签名:
public ActionResult sectors(string sectorName, int date, string seourl)
并且(以防万一),我也会为操作的参数分配默认值:
public ActionResult sectors(string sectorName, int date = 0, string seourl = string.Empty);