我喜欢创建一个注册路由,如下所示:
User/{^[a-zA-Z]+$}/{controller}/{action}/{id}
URL应该是这样的:
/User/myUser/Product/Action
所以我在AreaRegistration注册了一条路线。约束应该只允许单词。
context.MapRoute(
"Customer_Default",
"User/{^[a-zA-Z]+$}/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
- 我的约束正确吗
- 如何使用
RedirectToAction
路由重定向到此 - 在操作中获取用户名的最佳方式是什么?Regex
您需要使用MapRoute()
的constraints
参数
context.MapRoute(
"Customer_Default",
"User/{username}/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
constraints: new { username= @"^[a-zA-Z]+$" }
);
那么你的行动会是这样的:
public ActionResult ActionName(string username, int id)
- 我认为使用约束的正确方法是:路线。MapRoute(名称:"Customer_Default",url:"用户/{用户名}/{控制器}/}操作}/{id}",默认值:new{action="Index",id=UrlParameter.可选},限制:新的{username="^[a-zA-Z]+$"})
- 我不知道如何重定向到这条路线
- 我认为您最好创建一个登录模型,并使用一致类型来传递用户名和密码