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 6540689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:00:35+00:00 2026-05-25T11:00:35+00:00

I have an encoding problem I can’t seem to get to grips with regarding

  • 0

I have an encoding problem I can’t seem to get to grips with regarding a parameter on a route. Basically, it either never URL encodes or it double URL encodes. I just want it single-encoded!

Here’s the route I have registered:

routes.MapRoute("Scan", "Scan/{*url}", new { controller = "Scan", action = "Index" });

Now I have a controller action that redirects to that route. For simplicity’s sake, let’s say it looks like this:

var url = "troyhunt.com/search";
return RedirectToRoute("Scan", new { url });

The resulting URL is “Scan/troyhunt.com/search” – note that the forward slash after .com has not been escaped.

Now let’s try that again but apply URL encoding:

var url = HttpUtility.UrlEncode("troyhunt.com/search");
return RedirectToRoute("Scan", new { url });

This time the resulting URL is “Scan/troyhunt.com%252fsearch” – note that the forward slash has been double-encoded. If I set a breakpoint before the redirect I can see the UrlEncode method successfully encoding the forward slash into a %2f.

It appears that the redirect is encoding the “%” symbol but not the “/”. If I test with other characters typically escaped with URL encoding (?,=), the encoding is working just fine directly from the RedirectToRoute method. Could this be an issue with treating the slash as separating route segments therefore not escaping? Am I missing something else here?

Update: more info, commentary and workaround is discussed in To route or not to route, that is the question.

  • 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-25T11:00:36+00:00Added an answer on May 25, 2026 at 11:00 am

    Looking into System.Web.Routing it looks like this is happening due to the fact that route values are not URL encoded using HttpUtility.UrlEncode(value) but in fact, are “escaped” using Uri.EscapeUriString(value).

    The offending code is in System.Web.Routing.ParsedRoute:

    // Dev10 601636 Work around Uri.EscapeUriString not encoding #,&
        private static string UrlEncode(string str) {
            string escape = Uri.EscapeUriString(str); 
            return Regex.Replace(escape, "([#;?:@&=+$,])", new MatchEvaluator(EscapeReservedCharacters));
        }
    

    This could be a by design decision (would have to ask the Microsoft folks about that), due to the fact that forward slashes are a fairly integral part of routes. If it is I find it a little strange as the documentation for Uri.EscapeUriString() states that it’s main usage as:

    Use the EscapeUriString method to prepare an unescaped URI string to be a parameter to the Uri constructor.

    Following through the code that generates the route URL, it doesn’t seem to use the Uri class at all – the URL is constructed and returned in a string.

    If you passed this as a query string parameter, rather than as part of a specified route, it would be properly URL encoded (but would of course change the structure of your URL).

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

Sidebar

Related Questions

Problem: I can't seem to get FireFox to cache images sent from a dynamic
I'm having a problem with encoding from a wordpress feed that I just can't
I'm just starting to learn Ruby and have a problem with encoding; require 'rubygems'
I have a problem with encoding when writing XML files via QXmlStreamWriter in windows,
I have a problem trying to get my head around using UTF8 with Poco::XML::XMLWriter
I am having a problem with my encoding in Python. I have tried different
I have a problem now. I want to get wikipedia data using wikimeta. I
I have a convenience class for encoding URI's. In it I've created three methods
I have a file UTF-8 encoding in windows, and when i use it under
I have a simple code byte[] buffer = Encoding.UTF8.GetBytes(abracadabra); MemoryStream ms = new MemoryStream();

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.