12

I want to implement a SEO friendly URL for my ASP.NET MVC website.

Currently i have a URL like:

http://www.domain.com/product?id=productid

but now i want to rewrite my URL like:

http://www.domain.com/productname

So please anybody can help me on above...

1

5 Answers 5

14

This is the best article for starters -> SEO Friendly URls
The article also explain how to remove the whitespaces and dashes.

12

Please try with below solution. In global.asax.cs

routes.MapRoute(
    "Product",
    "{productName}",
    new { controller = "Product", action = "Index" },
    new { productName = UrlParameter.Optional  }
);

But you required to maintain uniqueness in productName and fetch record by that in index action of product controller (i.e in product Controller:

public ActionResult index(string productName)
{
     //do something regarding get product by productname
}
9

You can add a Route to your MVC routing engine in this fashion -

In Global.asax.cs

routes.MapRoute(
    "Product",
    "{controller}/{productId}/{productName}",
    new { controller = "Product", action = "Index" },
    new { productId = UrlParameter.Optional , productName = UrlParameter.Optional  }
);

This will allow you to have URL like

www.domain.com/productid/productname

The reason you may or may not be able to achieve

www.domain.com/productname

is that productname isn't an identifier and cannot be used to lookup a record uniquely. You would need an identifier in the url.

Ex - look at the URL for this question in SO, it has the ID and then appends SEO friendly test.

0
0

Create a new route in the Global.asax to handle this:

    routes.MapRoute(
        "productId",                  // Route name
        "productId/{id}/{name}",      // URL with parameters
        new { controller = "Home", action = "productId", id = 1234, name = widget }  // Parameter defaults
    );

Asp.Net MVC has routing built in, so no need for the Url Rewriter.

more details here

0

Copy the following code and past it in. { routes.MapRoute }

routes.MapRuote
{
   

       name: "productname",
       routeTempalate: "services {controller}{aciton}{id}
       defaults: new (id = RouteParameter.Optionl;)

}

But for you replace the "routeTemplate" with your requirements. for you it's will look like this:

routeTemplate: "services{controller}/{productname}"

I hope this will help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.