I want to change:
www.testurl.com/sports/blog/1
Where sports is my area, blog is my action and 1 is an ID of a blog post, to:
www.testurl.com/sports/blog/test-title-of-blog
Where blog is still my action but the id is not shown, but instead the title/permalink of the blog is.
Here is my AreaRegistration for this action:
context.MapRoute(
"sports",
"sports/{action}/{content}",
new { area = "Sports", controller = "Sports", action = "", content = "" });
Here is my action at the moment:
[HttpGet]
public ActionResult Blog(string content)
{
int contentId;
if (Int32.TryParse(content, out contentId))
{
model = service.GetBlogById(contentId);
}
else
{
model = service.GetBlogByTitle(content);
}
//Change URL to be: www.testurl.com/sports/blog/ + model.SEOFriendlyTitle
return View(model);
}
Users are able to search via the ID of the blog, but also by the title of it, but I only want the title to appear in the url bar, never the id.
I cannot do this via Redirect rules due to the continuing maintenance that would cause.
Is the controller the right place to do this? -Remember I may not have my title until after I retrieve it from the database using the ID
How would I go about changing the URL to display the title vs. the ID?
I think what you should do is return a RedirectResult to the new Url if the ID is numeric and is a valid contentId :
Of course, that will cause another round trip to the server but I can see a way to change the browser URL without a page redirect. You should also make sure that all published urls on your site are using the title instead of Id.
I hope it will help.
I suggest giving this a quick read.
http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls
If you are really can't have the ID in Url and don't want to do redirects then I think storing Url as Slugs in the database is the only other option.
*Some points if you are going to do this.*
Add a Unique Constraint to the column at the Database Level to avoid duplicates.
Create a Database Index on this column to speed up you reads.
So with this Url
This is your unique slug that you will query the database for instead of an ID