使用正则表达式匹配ASP.NET MVC的url并进行处理

04/14/2019 23:35:01 阅读(2164)

ASP.NET MVC url匹配及路由参考

routes.MapRoute(
	"NotFound",
	"{shortid:regex(^\\w{{8}}$)}",
	new { controller = "Home", action = "Detail" });
//匹配类似/xxxxxxxx这样的路径,返回/home/detail的内容

public async Task<IActionResult> Detail(string id)
{
	var shrotid = this.RouteData.Values["shortid"]?.ToString();
	...
	return View();
}
返回