Make URL language-specific (via routes)

后端 未结 2 1541
渐次进展
渐次进展 2020-12-14 12:13

Its hard to find an answer to this specific question, so ill pop it up here:

We want to build up our urls completely language specific, meaning

<
2条回答
  •  再見小時候
    2020-12-14 12:53

    I think there is more usefull solution istead of translating controller name, Prameter-less routing is very costmizable, modern and SEO frindly.

    The main idea is to store your route in database, with related controller, action and parameters. Then you can match that when user request any url.

    var httpContext = Request.RequestContext.HttpContext;
    if (httpContext == null) return;
    
    var routeData = routes.GetRouteData(httpContext);
    
    var slug = routeData.Values["slug"] as string;
    
    var urlRecord = urlRecordService.GetBySlugCached(slug);
    
    //process URL
    switch (urlRecord.EntityName.ToLowerInvariant()) {
      case "product":
        {
          data.Values["controller"] = "Product";
          data.Values["action"] = "ProductDetails";
          data.Values["productid"] = urlRecord.EntityId;
          data.Values["SeName"] = urlRecord.Slug;
        }
        break;
      case "category":
        {
          data.Values["controller"] = "Catalog";
          data.Values["action"] = "Category";
          data.Values["categoryid"] = urlRecord.EntityId;
          data.Values["SeName"] = urlRecord.Slug;
        }
        break;
        .....
    }

    This method is used in Nop-commerce you can download the source code and check this article NopCommerce ID-Less URL Structure

    The problem of this solution are: you should mentain your urls and performance.

提交回复
热议问题