2

I am currently trying to figure out how to change my blog's url rules.

Right now the URLs are /Blog/Details/1 but I read that it is a better SEO practice to make the URL /Blog/Details/Post-Title. I have created an extra field in my blog database labeled FriendlyUrl, and when I create a blog entry I replace spaces with dashes (-) but now I have no idea how to make my app work properly.

I was told to look to my global.asx.cs but this is what mine looks like.

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
    }
}

Here is my controller code for details

public ActionResult Details(int id = 0)
    {
        Blog blog = _db.Blogs.Find(id);
        if (blog == null)
        {
            return HttpNotFound();
        }
        return View(blog);
    }

and here is the link currently being used to link to a blog entry.

<a href="@Url.Action("Details", "Blog", new { id=item.Id})">@Html.DisplayFor(modelItem => item.Title)</a>

Thanks in advance for any help.

1 Answer 1

5

Put this route first in your app_start routing.cs file:

       routes.MapRoute(
            "Blog",
            "Blog/{id}",
            new { controller = "Details", action = "Blog", id=0 }
        );

So long as the Routing Engine finds this route first, any route that begins with Blog/ will be routed to Contoller Details and Action Blog.


If you want a more descriptive route that focuses on an SEO friendly slug, use this route:

       routes.MapRoute(
            "Blog",
            "Blog/{postid}/{slug}",
            new { controller = "Details", action = "Blog", id=0, slug="" } 
        );

any route that begins with Blog/{postid} will be routed to Contoller Details and Action Blog. When your action or action filters see a route that leaves the slug blank, look it up in the database and redirect your users to that URL.

So if, for example, you get a route like

/Blog/1287

you should redirect the user to

/Blog/1287/how-to-fix-your-routing-engine

This architecture is very similar to the design used by SO. Observe what happens if you try to go to

/questions/15593545/

you will find yourself at

/questions/15593545/asp-net-mvc4-custom-routing

Your Action method would now look like this

public ActionResult Blog (int postid, string slug)
{
Sign up to request clarification or add additional context in comments.

2 Comments

Can you expand a little on how I should modify my Controller and actual link. I am having issues getting it to work as described. Thanks again
@GregGoodwin, take a look at my revised answer. Does this help?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.