MVC 路由约束随机 a-zA-Z
MVC Routing constrain random a-zA-Z
我喜欢创建这样的注册路线:
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
路由重定向到这里?
- 在 Action 中获取用户名的最佳方式是什么?正则表达式...?
您需要利用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)
- 我认为使用约束的正确方法是:
routes.MapRoute(姓名: "Customer_Default",
url: "User/{username}/{controller}/{action}/{id}",
默认值:new { action = "Index", id = UrlParameter.Optional },
约束:新{用户名=“^[a-zA-Z]+$”});
- 我不知道如何重定向到这条路线..
- 我认为你最好创建一个登录模型,并使用一致的类型来传递用户名和密码。
我喜欢创建这样的注册路线:
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
路由重定向到这里? - 在 Action 中获取用户名的最佳方式是什么?正则表达式...?
您需要利用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)
- 我认为使用约束的正确方法是: routes.MapRoute(姓名: "Customer_Default", url: "User/{username}/{controller}/{action}/{id}", 默认值:new { action = "Index", id = UrlParameter.Optional }, 约束:新{用户名=“^[a-zA-Z]+$”});
- 我不知道如何重定向到这条路线..
- 我认为你最好创建一个登录模型,并使用一致的类型来传递用户名和密码。