How to pass special characters so ASP.NET MVC can handle correctly query string data?

前端 未结 3 1370
陌清茗
陌清茗 2020-12-29 07:58

I am using a route like this one:

routes.MapRoute(\"Invoice-New-NewCustomer\",
    \"Invoice/New/Customer/New/{*name}\",
    new { controller = \"Customer\",         


        
相关标签:
3条回答
  • 2020-12-29 08:36

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

    Server.URLencode(strURL)
    

    C# will become "c%23".

    0 讨论(0)
  • 2020-12-29 08:37

    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.

    0 讨论(0)
  • 2020-12-29 08:46

    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

    0 讨论(0)
提交回复
热议问题