采取以下控制器操作
public ActionResult NextBySURNAME(int id, string data)
{
//code to process the data and edit the id accoringly not written yet
return RedirectToAction("Edit", new { id = id });
}
Run Code Online (Sandbox Code Playgroud)
如果我用/ Mycontroller/NextBySURNAME/12/Smith%20Simon调用它
然后它工作正常(在这种情况下编辑记录12)但是
/ myController的/ NextBySURNAME/12 /史密斯%20
给了我一个404
现在我知道在某些情况下我的问题域尾随空白是很重要的,所以我不只是想修剪它.那为什么这会破坏我的路线呢?
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/{data}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional, data=UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud) 我使用HttpUtility.UrlEncode来编码路由中使用的任何值.
我已经解决了编码正斜杠的问题.我现在遇到的新问题是空格.空格编码为+.
这适用于VS集成的Web服务器,但我在Windows Server 2008上的IIS7中遇到了问题.如果我有URL http://localhost/Home/About/asdas+sdasd
我得到错误404.11 - 请求包含双转义序列.
我知道我可以用"%20"替换空格,但我不想关心自己的编码器编码.有没有准备好使用UrlEncoder for MVC?
ASP.NET在请求路径中"正常化"反斜杠以转发斜杠,我需要它们作为反斜杠(它用于在数据库中执行查找).我不介意如果没有转义,转义为正斜面的转义是不是这个问题.
config.Routes.MapHttpRoute(
name: "TransactionsApi",
routeTemplate: "api/transactions/{*transaction}",
defaults: new { controller = "transactions", transaction = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
请注意,我已设置事务以匹配路径的其余部分.
我尝试了以下URL(来自浏览器和Fiddler):
api/transactions/mscorlib.pdb\DFA83312EAB84F67BD225058B188F22B1\mscorlib.pdbapi/transactions/mscorlib.pdb\\DFA83312EAB84F67BD225058B188F22B1\\mscorlib.pdbapi/transactions/mscorlib.pdb%5CDFA83312EAB84F67BD225058B188F22B1%5Cmscorlib.pdbapi/transactions/mscorlib.pdb%5C%5CDFA83312EAB84F67BD225058B188F22B1%5C%5Cmscorlib.pdb当他们点击我的Web API方法时,他们就是全部mscorlib.pdb/DFA83312EAB84F67BD225058B188F22B1/mscorlib.pdb.我检查了当前的情况HttpContext,看起来ASP.NET正在进行这种规范化(而不是MVC4).
可能的解决方案:
{*transaction}部分,如果它包含一个反斜杠.不是真正的地址栏hackable有关如何让ASP.NET 不进行此规范化的任何想法?
使用像http://abc.com/myid/ab%2fcd这样的URL (其中%2f是转义斜杠),asp.net将从我的应用程序的角度(以及从asp.net mvc的角度来看)取消%2f )URL是:http://abc.com/myid/ab/cd
由于我的应用程序使用asp.net mvc,如果我想要指定类似"/ myid/{id}"的路由,这种行为很容易导致路由问题,因为asp.net的unescaping将导致该路由不匹配.
根据这个问题的答案:URL中的URL编码斜杠并根据这个msdn页面:http://msdn.microsoft.com/en-us/library/ee656542.aspx解决方案(在.Net 4.0中)是将以下内容放在web.config中:
<uri>
<schemeSettings>
<add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes"/>
</schemeSettings>
</uri>
Run Code Online (Sandbox Code Playgroud)
但我实际上无法让它工作 - "%2f"仍然被自动转换为"/".有谁知道为什么配置设置可能不适合我,或有任何其他建议?
我有一个ASP.NET MVC站点,我想要路由/{controller}/{id}/{action}/{date},其中"date"是日期/时间的mm/dd/yyyy部分.(我正在处理时间尺寸的数据,所以我需要一个ID和一个时间点来完成大多数操作)
这个的路线很简单:
routes.MapRoute(
"TimeDimensionedRoute",
"{controller}/{id}/{action}/{date}",
new { controller = "Iteration", action = "Index", id = String.Empty, date = String.Empty }
);
Run Code Online (Sandbox Code Playgroud)
此路线正确地将" / Foo/100/Edit/01%2F21%2F2010 " 映射到所需的操作.更新:这是不正确的.这没有正确路由,我错了.请参阅已接受答案中链接的相关问题.
我的问题是,当我使用Html.ActionLink()生成此路由的链接时,它不会对日期进行URL编码,最终会出现无效的URL,例如" / Foo/100/Edit/01/21/2010 ".
有没有办法让路由基础设施为我编码值?我必须手动对我传递给HTML帮助程序的数据进行URL编码似乎是错误的.
我有一个控制器,其方法有一个在路由中传递的变量 - 控制器:
\n\n[Route("cont/{MyParameter}")]\npublic ActionResult Index(string MyParameter)\n{\n...\n}\nRun Code Online (Sandbox Code Playgroud)\n\n当我像这样在 URL 中传递一个值 (abcdef) 时,就可以了:
\n\nhttp://localhost/my-website/cont/abcdef\nRun Code Online (Sandbox Code Playgroud)\n\n但是,当我传递带有编码斜杠的一个时,它失败了,就好像斜杠没有编码一样,例如:
\n\nhttp://localhost/my-website/cont/abc%2fdef\nRun Code Online (Sandbox Code Playgroud)\n\n我在 IIS 中收到“无法找到资源”的消息 - 请求的 URL 显示在以下内容中:
\n\nRequested URL\n\xc2\xa0\xc2\xa0\xc2\xa0http://localhost:80/f-website/cont/abc/def\nRun Code Online (Sandbox Code Playgroud)\n\n它似乎添加了斜杠 - 尽管它们已被编码。
\n\n但地址栏仍然显示原始 URL,其中包含 %2f。
\n\n如何将 / 作为参数传入?我认为 URL 编码是这里的解决方案,但它显然没有效果。
\n\n我在谷歌上找不到任何关于这个具体问题的信息。
\n我有IIS(Microsoft-IIS/7.5)返回403禁止,我无法弄清楚为什么.我把它缩小到了,%2F但只有当它前面有一个字母时.知道是什么原因引起的吗?
这些工作......
但是如果你把任何一个字母放在%2F它前面那就失败了403.
这些失败......
谢谢!
更新:我排除了ColdFusion,因为这给出了相同的403:http://example.com/mySite123/indexdotcfm? x = a%2F
更新:
Top Level IIs:
Checked:
Allow unlisted file name extensions
Allow unlisted verbs
Allow high-bit characters
Unchecked:
Allow double escaping
Request Limits:
Maximum allowed content length (Bytes): 30000000 Maximum URL length (Bytes):
4096 Maximum query string (Bytes): 2048
Sites
mySite123:
Checked:
Allow unlisted verbs
Allow high-bit characters
Unchecked:
Allow unlisted file name …Run Code Online (Sandbox Code Playgroud) www.yoursite.com/image/ http://images.google.com.ph/images/nav_logo7.png
我需要知道的是Controller Action和Global.asax路由
asp.net-mvc ×5
c# ×3
asp.net ×2
.net ×1
coldfusion ×1
escaping ×1
iis ×1
java ×1
routing ×1
url ×1
url-routing ×1
urlencode ×1