【问题标题】:ASP.NET MVC. Routing manager. Resolve routes issues with similar link valuesASP.NET MVC。路由管理器。解决具有相似链接值的路由问题
【发布时间】:2016-04-04 04:31:40
【问题描述】:

我需要有关最近遇到的问题的建议。问题的本质:在我的网站上我有一个Controller。让它成为ProductController。当然,我还有一些Actions

public class ProductController : Controller
{ 
 public virtual ActionResult ProductDetails(string slug)
    public virtual ActionResult ProductList (string slug, string category, string filter)
}

Filter 参数可以为空。我在RouteConfig的路线:

routes.MapRoute("ProudctsList", "products/{slug}/{category}/{selectedFilters}", MVC.Product.ProductList()
                .AddRouteValue("selectedFilters", ""));
routes.MapRoute("Products", "products/{slug}", MVC.Product.ProductDetails());
routes.MapRoute("CustomPage", "custom/{slug}", MVC.CustomPage.Index());
......
routes.MapRoute("BrandCollectionDetails", "brand/{slug}/{collectionId}", MVC.Brand.BrandCollectionDetails());    
routes.MapRoute("BrandDetails", "brand/{slug}", MVC.Brand.BrandDetails());
routes.MapRoute("HomePage", "", MVC.Home.Index());                   
routes.MapRoute("Sales.Index", "sales", MVC.Sales.Index());

根据要求,我需要从Url 中删除products。我的意思是,如果Url 有这样的方案产品/汽车/奥迪/黑色 - 它应该只是汽车/音频/黑色。而且我还需要从Url 中删除custom。如您所见,我将使用相同的方案获得Urls。我需要说我有我List的自定义Urls

List<string> customPages = new List<string> {/Ferrari, ...}

我的意思是所有Urls 与这样的方案:/{something} 应该使用这个List 检查。 我只需要从 Urls products/{slug}/{category}/{filter}custom 中删除 products/ 段,然后将用户引导到必要的 Controller Action 。我不知道如何构建应该解决此问题并需要专家帮助的自定义路由管理器。请告诉我问题是否清楚。

【问题讨论】:

    标签: c# asp.net asp.net-mvc routing asp.net-mvc-5


    【解决方案1】:

    有几种方法可以解决这个问题。看起来您的特定问题可以通过创建 IRouteConstraint 的实现来解决。

    这将允许您动态覆盖应用程序的默认路由功能。

    public class ProductUrlConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            List<string> customPages = new List<string> {"Ferrari", "Porche"};
    
            if (values[parameterName] != null)
            {
                var slug= values[parameterName].ToString();
                var product = customPages.Where(p => p == slug).FirstOrDefault();
                if(!string.isNullorEmpty(product))
                {
                     HttpContext.Items["customProduct"] = product;
                     return true;
                }
            }
            return false;
        }
    }
    

    在路由定义中使用 IRouteConstraint 的实现,如下所示:

    routes.MapRoute(
        name: "ProductRoute",
        url: "{*customProduct}",
        defaults: new {controller = "Product", action = "Index"},
        constraints: new { customProduct= new ProductUrlConstraint() }
    );
    
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    然后该操作将如下所示:

    public ActionResult Index(string permalink)
    {
        var page = HttpContext.Items["customProduct"].ToString();
        //dont forget to check for null :)
        //model/ view logic
    }
    

    根据您以后的要求,您可能需要在操作中进行一些自定义字符串处理。

    将自定义产品存储在 HttpContext 中只是性能考虑。

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 2011-11-30
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多