【问题标题】:MVC Routing constrain random a-zA-ZMVC 路由约束随机 a-zA-Z
【发布时间】:2015-02-25 10:18:00
【问题描述】:

我喜欢创建这样的注册路线:

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 }
);
  1. 我的约束是否正确。
  2. 如何使用RedirectToAction 路由重定向到此?
  3. 在操作中获取用户名的最佳方式是什么?正则表达式...?

【问题讨论】:

    标签: c# regex asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    你需要利用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)
    

    【讨论】:

      【解决方案2】:
      1. 我认为使用约束的正确方法是: routes.MapRoute(name: "Customer_Default", url: "用户/{username}/{controller}/{action}/{id}", 默认值:new { action = "Index", id = UrlParameter.Optional }, 约束:新 { 用户名 = "^[a-zA-Z]+$" });
      2. 我不知道如何重定向到这条路线..
      3. 我认为你最好创建一个登录模型,并使用一致的类型来传递用户名和密码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        相关资源
        最近更新 更多