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...
But to ensure the URL to be SEO friendly we need to change the “id” to contain a name. There can be multiple records with the same name existing in the database. That creates an issue while using a name of a record as “id” or key. Here, I will be using the name and id combination to get the uniqueness for the view.
URLs that are simple, easy to read, and include keywords that describe the content on a web page are SEO-friendly. For example, if you're searching for information about pancakes, a URL like https://en.wikipedia.org/wiki/pancake will help you decide to click on that link.
SEO friendly URLs are URLs that are designed to meet the needs of users and searchers. Specifically, URLs optimized for SEO tend to be short and keyword-rich.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With