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

前端 未结 3 1372
陌清茗
陌清茗 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: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.

提交回复
热议问题