如何通过特殊字符,所以ASP.NET MVC能够正确处理查询字符串数据? [英] How to pass special characters so ASP.NET MVC can handle correctly query string data?

查看:223
本文介绍了如何通过特殊字符,所以ASP.NET MVC能够正确处理查询字符串数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用像这样的路线:

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

当我称之为返回RedirectToAction(NewInvoice,客户,新的{名}); 和名称等于C#的家伙,姓名参数被截断以C。​​

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".

所以我的问题是:什么是处理这种使用ASP.NET MVC

So my question is : What is the best way to handle this kind of special character with ASP.NET MVC?

谢谢!

推荐答案

好吧,我确认这是的现在的在ASP.NET路由一个已知​​的问题,很遗憾。问题是,深路由的深处,我们使用Uri.EscapeString转义开放的路由参数时。然而,这种方法不逃避#字符。

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.

注意#字符(即井号)在技术上是错误的字符。 C♯的语言其实是一个C,其次是夏普迹象,因为在音乐:<一href=\"http://en.wikipedia.org/wiki/Sharp_%28music%29\">http://en.wikipedia.org/wiki/Sharp_(music)

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)

如果您使用了锐利的迹象,这可能会解决这个问题。 :P

If you used the sharp sign, that could potentially solve this problem. :P

另一种解决方案,因为大多数人将要使用的井号是写这条路线,并获取虚拟路径路径后,连接code。使用HttpUtility.UrlEn code#号这一个带自定义路由codeS#至23%。

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.

作为后续行动,我想你点这个博客帖子里面谈到过其他非法字符。 <一href=\"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

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

这篇关于如何通过特殊字符,所以ASP.NET MVC能够正确处理查询字符串数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆