代码之家  ›  专栏  ›  技术社区  ›  labilbe

如何传递特殊字符以便ASP。NET MVC可以正确处理查询字符串数据吗?

  •  15
  • labilbe  · 技术社区  · 17 年前

    我使用这样的路线:

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

    有一个操作可以处理此路由:

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

    当我打电话的时候 return RedirectToAction("NewInvoice", "Customer", new {name}); name等于“The C#Guy”,“name”参数被截断为“The C”。

    所以我的问题是:用ASP处理这种特殊字符的最佳方法是什么。网络MVC?

    谢谢!

    3 回复  |  直到 15 年前
        1
  •  16
  •   Haacked    16 年前

    好的,我确认了这是 现在 ASP中的一个已知问题。NET路由,不幸的是。问题是,在路由的深处,我们在转义Uri的路由参数时使用Uri.EscapeString。但是,该方法不会转义“#”字符。

    请注意,#字符(又名Octothorpe)在技术上是错误的字符。C语言实际上是一个“C”,后面跟着一个夏普符号,就像音乐一样: http://en.wikipedia.org/wiki/Sharp_(music)

    如果你使用尖锐的标志,这可能会解决这个问题。P

    另一种解决方案是,由于大多数人都想使用octothorpe,因此可以为此路由编写一个自定义路由,并在获得虚拟路径路径后,使用HttpUtility对#符号进行编码。UrlEncode,将#编码为%23。

    作为跟进,我想向您推荐这篇关于传递其他“无效”字符的博客文章。 http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx

        2
  •  3
  •   EndangeredMassa    17 年前

    URL编码!更改链接,使其对特殊字符进行编码。

    Server.URLencode(strURL)
    

    C#将变为“C%23”。

        3
  •  3
  •   Haacked    17 年前

    在我的机器上工作。以下是我为创建最简单的示例所做的工作。

    //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;
            }
        }
    }
    

    然后我启动了我的应用程序并导航到/home/index。重定向发生了,我在浏览器中看到了“C#家伙”。