asp.net-mvc 如何传递特殊字符以便 ASP.NET MVC 可以正确处理查询字符串数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/373599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass special characters so ASP.NET MVC can handle correctly query string data?
提问by labilbe
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".
当我调用return RedirectToAction("NewInvoice", "Customer", new {name});并且 name 等于“The C# Guy”时,“name”参数被截断为“The C”。
So my question is : What is the best way to handle this kind of special character with ASP.NET MVC?
所以我的问题是:使用 ASP.NET MVC 处理这种特殊字符的最佳方法是什么?
Thanks!
谢谢!
回答by Haacked
Ok, I confirmed that this is nowa 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.
好吧,我确认这是现在在ASP.NET路由一个已知的问题,很遗憾。问题是在路由的深处,我们在转义 Uri 的路由参数时使用了 Uri.EscapeString。但是,该方法不会转义“#”字符。
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)
请注意,# 字符(又名 Octothorpe)在技术上是错误的字符。C?该语言实际上是一个“C”,后跟音乐中的 Sharp 符号:http: //en.wikipedia.org/wiki/Sharp_(music )
If you used the sharp sign, that could potentially solve this problem. :P
如果您使用尖号,则可能会解决此问题。: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.
另一种解决方案,因为大多数人会想要使用八色法是为此路由编写自定义路由,在获取虚拟路径路径后,使用 HttpUtility.UrlEncode 对 # 符号进行编码,将 # 编码为 %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
作为后续,我想向您指出这篇博客文章,该文章讨论了传入其他“无效”字符。http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx
回答by EndangeredMassa
URL Encoding! Change the link so that it encodes special characters.
网址编码!更改链接,使其对特殊字符进行编码。
Server.URLencode(strURL)
C# will become "c%23".
C# 将变成“c%23”。
回答by Haacked
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.
然后我启动了我的应用程序并导航到 /home/index.html 。重定向发生,我在浏览器中看到了“The C# Guy”。

