【问题标题】:How do I create a constraint for this url route?如何为此 url 路由创建约束?
【发布时间】:2011-05-02 20:53:28
【问题描述】:
   routes.MapPageRoute("View Home Page",
        "{homepage}",
        "~/home.aspx",
        true,
        new RouteValueDictionary { { "homepage", "national" } },
        new RouteValueDictionary { { "region", "^(national|bc|ab|on){0,1}$" } }
       );

我只希望路由匹配 http://www.mydomain.com/nationalhttp://www.mydomain.com/bchttp://www.mydomain.com/abhttp://www.mydomain.com/on 等 url

如何构造约束?

【问题讨论】:

    标签: c# asp.net regex url-routing


    【解决方案1】:

    我最终编写了一个自定义约束,所以我的代码现在看起来像这样:

    routes.MapPageRoute("View Home Page",
            "{region}/default.aspx",
            "~/home.aspx",
            true,
            new RouteValueDictionary { { "region", "national" } },
            new RouteValueDictionary { { "region", new HomePageConstraint() } }
           );
    

    和班级:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Routing;
    
    /// <summary>
    /// Summary description for HomePageConstraint
    /// </summary>
    public class HomePageConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return GetRegions().Any(x => x.ToLower() == values[parameterName].ToString().ToLower());
    
        }
    
    
        private List<string> GetRegions()
        {
            List<string> set = new List<string>();
            set.Add("National");
            set.Add("BC");
            set.Add("AB");
            set.Add("SASK");
            set.Add("MAN");
            set.Add("ON");
            set.Add("QC");
            set.Add("Maritimes");
            set.Add("NL");
    
            return set;
    
        }
    }
    

    效果很好(我使用的是 webforms 而不是 mvc - 我确信它在两种情况下都有效)。在此处找到方法:http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx

    【讨论】:

      【解决方案2】:

      还没有回复,所以我会让你知道我做了什么。不幸的是,ASP.NET MVC 的路由引擎并不是最好的。

      您需要更改适当的变量以匹配您的路由和视图,但这应该可以帮助您入门。

      将此作为您要映射的最后一个路线:

      routes.MapRoute("Root", "{pathName}", new
      { 
          controller = "Root", 
          action = "Default"
      });
      

      在你的控制器中:

      public class RootController : Controller
      {
      
          [HttpGet]
          public ActionResult Default(string pathName)
          {
              // No pathName? 
      
              if (String.IsNullOrWhiteSpace(pathName)) {
                  return this.View("~/Views/Root/home.aspx")
              }
      
              // Check to see if there's a view for the pathName
      
              var viewPath = String.Format("~/Views/Root/{0}.aspx", pathName);
      
              if (File.Exists(Server.MapPath(viewPath))) {
                  return this.View(viewPath);
              }
      
              // Not found
      
              this.Response.StatusCode = System.Net.HttpStatusCode.NotFound;
              return this.View("~/Views/404.aspx");
          }
      
      }
      

      因此,您将不得不以您期望的“路径名”来命名您的视图:

      • ~/Views/Root/national.aspx
      • ~/Views/Root/ab.aspx
      • ~/Views/Root/bc.aspx

      如果你不介意有这样的约定,那还不错。

      【讨论】:

        猜你喜欢
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        相关资源
        最近更新 更多