Question

I want to be able to rewrite my URL's to a single level URL. This means that i need to make a dynamic(will change in each language) rewrite rule, like this:

Original Url: http://www.mydomain.com/account/pages/13

I always want to show the URL in a single level:

http://www.mydomain.com/my-page-title

never no more than one slash / after the domain name.

I also need to be able to translate the page title in the above example:

http://www.mydomain.com/my-translated-page

How do I achieve this, and it should be able to change this on runtime - that is to "improve" the url, just like rewrite rules in htaccess

Was it helpful?

Solution

You have to create your own Routes. As you are using the MVC4 put this custom route above the Default Route of the Application.

 routes.MapRoute(
      name: "Custom_Route",
      url: "My-Page-Title/{id}",
      defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
 );

And for translating a Url in Mvc

OTHER TIPS

You can create you own custom routes like this, This should be the first route

routes.MapRoute(
                name: "Default_Custom",
                url: "MyCustom-{action}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

and make sure that you have this line in Global.asax

protected void Application_Start()
{
  RouteConfig.RegisterRoutes(RouteTable.Routes);
}

This woks for the all request like @Html.ActionLink("Home", "Index", "Home")

But the request like

@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })

Will be handled by the default route

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

That's not a simple and easy to answer question. Complete description can't be placed here. But I try to guide you with some suggestions and Links may be useful. Considering that with The default form of MVC Routing is /controller/action/id, we'll ensure uniqueness of each part during programming and help of database's unique Ids, In this way, no need to pay attention on uniqueness of each part And routing is infallible always.

By the way, First of all, ensure you'll generate non-repetitive URLs to be caused ambiguity in Routing. So each Page title should be unique!

After that you need to have lower cased and dashed page titles. Or write following code to put the title to lower case and replace non-alphanumeric characters with dashes to employ in routing implementation.

public static string SeoName(string name)
{
    return Regex.Replace(name.ToLower().Replace(@"'", String.Empty), @"[^\w]+", "-");
}

Finally you need to have Translation provider Interface to use in Routing. See Translating routes Article to implement this feature too.

Note that you must order routes according to their priority as desired to perform in order subsequently.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top