16

I am using a route like this one:

routes.MapRoute("Invoice-New-NewCustomer",
    "Invoice/New/Customer/New/{*name}",
    new { controller = "Customer", action = "NewInvoice" },
    new { name = @"[^\.]*" });

There is an action which handles this route:

public ActionResult NewInvoice(string name)
{
    AddClientSideValidation();
    CustomerViewData viewData = GetNewViewData();
    viewData.InvoiceId = "0";
    viewData.Customer.Name = name;
    return View("New", viewData);
}

When I call return RedirectToAction("NewInvoice", "Customer", new {name}); and name is equal to "The C# Guy", the "name" parameter is truncated to "The C".

So my question is : What is the best way to handle this kind of special character with ASP.NET MVC?

Thanks!

3 Answers 3

16

Ok, I confirmed that this is now a known issue in ASP.NET Routing, unfortunately. The problem is that deep in the bowels of routing, we use Uri.EscapeString when escaping routing parameters for the Uri. However, that method does not escape the "#" character.

Note that the # character (aka Octothorpe) is technically the wrong character. C♯ the language is actually a "C" followed by a Sharp sign as in music: http://en.wikipedia.org/wiki/Sharp_(music)

If you used the sharp sign, that could potentially solve this problem. :P

Another solution, since most people will want to use the octothorpe is to write a custom route for this route and after getting the virtual path path, encode the # sign using HttpUtility.UrlEncode which encodes # to %23.

As a follow-up, I wanted to point you to this blog post which talks about passing in other "invalid" characters. http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Phil for taking the time to investigate this problem
3

URL Encoding! Change the link so that it encodes special characters.

Server.URLencode(strURL)

C# will become "c%23".

Comments

3

Works on my machine. Here's what I did to create the simplest possible example.

//Global.asax.cs

using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication4 {
  public class MvcApplication : System.Web.HttpApplication {
    public static void RegisterRoutes(RouteCollection routes) {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
        "Default",                        // Route name
        "{controller}/{action}/{id}",               // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
      );

      routes.MapRoute("Invoice-New-NewCustomer",
            "Invoice/New/Customer/New/{*name}",
            new { controller = "Customer", action = "NewInvoice" },
            new { name = @"[^\.]*" });
    }

    protected void Application_Start() {
      RegisterRoutes(RouteTable.Routes);
    }
  }
}

//HomeController.cs
using System.Web.Mvc;

namespace MvcApplication4.Controllers {
  [HandleError]
  public class HomeController : Controller {
    public ActionResult Index() {
      return RedirectToAction("NewInvoice", "Customer", new { name = "The C# Guy" });
    }
  }
}

//CustomerController.cs
using System.Web.Mvc;

namespace MvcApplication4.Controllers {
    public class CustomerController : Controller {
        public string NewInvoice(string name) {
            return name;
        }
    }
}

I then started my app and navigated to /home/index. THe redirect occurs and I saw "The C# Guy" in my browser.

1 Comment

Is it redirecting to Invoice/New/Customer/New/The+C%2523+Guy or to Customer/NewInvoice?name=The%2BC%2523%2BGuy ?

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.