Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:36:59+00:00 2026-05-22T12:36:59+00:00

How do you redirect a request in ASP.NET MVC to its correct canonical version

  • 0

How do you redirect a request in ASP.NET MVC to its correct canonical version if part of the URL is missing?

Using Stack Overflow as an example, the site adds the question title to the end of its routes, but uses the question ID in the route to actually find the question. If the title gets omitted you will be redirected to the correct URL.

For example, visiting the URL:

stackoverflow.com/questions/9033 

will redirect to

stackoverflow.com/questions/9033/hidden-features-of-c

How does this work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:36:59+00:00Added an answer on May 22, 2026 at 12:36 pm

    First create a route:

    routes.MapRoute( 
        "ViewProduct", 
        "Products/{id}/{productName}", 
        new { controller = "Product", action = "Details", id = "", productName = "" } 
    );
    

    Then create the Action method like so:

    public ActionResult Details(int? id, string productName) 
    { 
        Product product = ProductRepository.Fetch(id); 
    
        string realTitle = UrlEncoder.ToFriendlyUrl(product.Title); 
        string urlTitle = (productName ?? "").Trim().ToLower(); 
    
        if (realTitle != urlTitle)
        { 
            string url = "/Products/" + product.Id + "/" + realTitle; 
            return new PermanentRedirectResult(url);
        } 
    
        return View(product); 
    }
    

    You’re basically comparing the entity title in the URL with the one stored in the database, if they don’t match then perform a 301 permanent redirect. Make sure it’s a ‘permanent’ redirect (301 status code) instead of a temp redirect (302). This way search engines will treat it as a permanent change of the URL and will update their indexes accordingly, this might happen if the title of your entity changes after a search engine has indexed it (e.g. someone changes the name of the product).

    Another thing to be aware of, if your title allows any free text, you need to strip out any characters that are invalid for a URL, and make it more readable for humans and search engines alike, hence the UrlEncoder.ToFriendlyUrl method in the code above, the implementation is below:

    public static class UrlEncoder 
    { 
        public static string ToFriendlyUrl (this UrlHelper helper, 
            string urlToEncode) 
        { 
            urlToEncode = (urlToEncode ?? "").Trim().ToLower(); 
    
            StringBuilder url = new StringBuilder(); 
    
            foreach (char ch in urlToEncode) 
            { 
                switch (ch) 
                { 
                    case ' ': 
                        url.Append('-'); 
                        break; 
                    case '&': 
                        url.Append("and"); 
                        break; 
                    case '\'': 
                        break; 
                    default: 
                        if ((ch >= '0' && ch <= '9') || 
                            (ch >= 'a' && ch <= 'z')) 
                        { 
                            url.Append(ch); 
                        } 
                        else 
                        { 
                            url.Append('-'); 
                        } 
                        break; 
                } 
            } 
    
            return url.ToString(); 
        } 
    }
    

    So when you write out the URLs into the View, be sure to encode the titles with this method e.g.

    <a href="/Products/@Model.Id/@Url.ToFriendlyUrl(Model.Title)">@Model.Title</a>
    

    I’ve written a blog post about this here http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ASP.NET 2.0 C#. I want to redirect all request for my
I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually
I am using ASP.NET MVC 1.0. I have an ActionResult which receives a form
IIS 7.5 MVC 2.0 ASP.NET 4.0 If my MVC site getting external request for
I use request validation on asp.net mvc 2 on .NET 4, and whenever I
We employ Out-of-Process SQL Session State, ASP.NET 3.5 MVC 1.0 and Forms Authentication using
I want to redirect a request in an asp.net web site based on the
Im working on a multilingual site using asp.net mvc, and im wondering how search
I'm trying to redirect/forward a Pylons request. The problem with using redirect_to is that
I just installed the ASP.Net MVC Preview 2 so I can have a look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.