tags:

views:

3581

answers:

7

Hello,

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!

+1  A: 

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

Server.URLencode(strURL)

C# will become "c%23".

EndangeredMassa
+1  A: 

If I encode the URL i have the following URL

http://localhost:1978/Invoice/New/Customer/New/The+C%2523+Guy

And the web server returns

Server Error in '/' Application.
HTTP Error 400 - Bad Request.
Version Information: ASP.NET Development Server 9.0.0.0 

labilbe
+1  A: 

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.

Haacked
Is it redirecting toInvoice/New/Customer/New/The+C%2523+Guyor toCustomer/NewInvoice?name=The%2BC%2523%2BGuy?
labilbe
Ah, I see. I'll look into it.
Haacked
+9  A: 

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

Haacked
Thanks Phil for taking the time to investigate this problem
labilbe
A: 

string encodedUrl=Server.UrlEncode("My first C# Asp.Net MVC Application");

it gives me something like this "My+first+C%2523+Asp.Net+MVC+Application"

empty spaces replaced by "+" but i think it should be "%20"

any ideas, how can i resolve this??

Kind Regards, Saurabh

A: 

It doesn't encode the " * " also

Have you found a solution to this? I need to be able to handle (*) in wildcard searches, I get the 400 Error as well.
shanabus
A: 

Is this issue been resolved? Hoping anyone can offer the solution. Thank You.

imperialx