Language Dependencies in ASP.NET MVC

Has anyone had any experience with language dependent routes with ASP.NET MVC? I would like to make a localized URL to improve SEO. For example, http://mysite.com/products/cars will map to the same controller / action as http://mysite.com/produkter/bilar ?

I tried browsing a bit, but I could not find anything like it. I'm not even sure if this is really such a good idea, but I would suggest that it helps SEO when users search in their own language. I would suggest that this would require some tuning of the mvc route mechanism.

Edit: Mato definitely has the best solution so far, I would like to see a special RouteHandler solution, though for reference.

+3
source share
4 answers

You can implement your own factory controller class, which translates the name of the controller before initializing it. For example, you can store translations in a resource file or in a database. The easiest way to do this is to inherit from DefaultControllerFactory and overwrite the CreateController function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    class CustomControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            /**
             * here comes your code for translating the controller name 
             **/

            return base.CreateController(requestContext, controllerName);
        }
    }
}

The final step would be to register the implementation of your factory controller when the application starts (in Global.asax).

namespace MyApplication
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ControllerBuilder.Current.SetControllerFactory(typeof(CustomControllerFactory));
        }
    }
}
+3
source

One thing with SEO is that if a search engine finds two identical documents on two different links, this can reduce the page rank for one of the pages. Then you need to translate the contents of the page.

+3
source

(http://iridescence.no/post/Defining-Routes-using-Regular-Expressions-in-ASPNET-MVC.aspx), -

routes.Add(new RegexRoute(@"^(Products|Produkter)$", new MvcRouteHandler())
{
    Defaults = new RouteValueDictionary(new { controller = "Products"  })
});
+1

Regex, , URL ( -).

, @Runeborg, . - MVC 5 ( <) , , .

:

  • URL- ( MVC5 , , - ).
  • Attribute .
  • URL (Html.ActionLink / Url.Action ).

. .

.

0

Source: https://habr.com/ru/post/1717130/


All Articles