我正在尝试为搜索页面创建语义URL,但如果有人使用点完成的搜索,则.net引擎返回404.
请求甚至没有到达路由引擎,所以我认为它与安全性或类似的东西有关.
例如,stackoverflow路由在这些情况下也不起作用: /sf/ask/tagged/etc/.
我有一条路线:
[Route("/usergroup/{User}/{Group}", "GET")]
Run Code Online (Sandbox Code Playgroud)
问题是当{User}中存在特殊字符(例如句点)时,路径未正确评估.如果我手动测试请求,应如何编码?
/usergroup/joe.smith/group1 of course doesn't work
/usergroup/joe%2Esmith/group1 doesn't work either
Run Code Online (Sandbox Code Playgroud)
这里的诀窍是什么?
(更新)这似乎只发生在某些特殊字符上.我可以使用%2D( - )但我不能使用%2E(.).
(更新2)这似乎比那更险恶.这似乎只发生在Visual Studio启动的ASP.Net Development服务器上.
(更新3)这使调试很痛苦.基本上,路径路径不能包含句点,或者我得到"Handler for Request not found"404.
实际上,ServiceStackHttpHandlerFactory(3.9.59)似乎抛出了未找到的错误.
如果我有以下路线:
{controller}/{action}/{id}
Run Code Online (Sandbox Code Playgroud)
我查看以下网址:
c1/a1/abc.
Run Code Online (Sandbox Code Playgroud)
它不起作用.仅当句点位于URL的末尾时才会发生这种情况.有什么办法让ASP.NET MVC识别出作为ID的一部分?
我有一个MVC5 Web应用程序,其中一些ID可以包含句点字符.
例如
这工作正常 http://www.website.com/Employee/Details/123
这将失败 http://www.website.com/Employee/Details/123.45
但这样可以正常工作 http://www.website.com/Employee/Details/?id=123.45
类似的问题:
目前我找到的所有解决方案都在上面的链接中描述.
<httpRuntime relaxedUrlToFileSystemMapping="true" />
<add name="UrlRoutingHandler" type="System.Web.Routing.UrlRoutingHandler, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" path="Remote.mvc/*" verb="GET"/>
由于用户只会通过网页中的菜单和链接浏览这些网址,我希望ActionLink html帮助程序能够足够聪明地呈现?id = 123.45,因为它识别出Id包含Id路由值的不安全字符.
或者有没有办法强制ActionLink html助手始终在查询字符串中呈现Id路由值?
更新 我找不到任何参数来强制ActionLink呈现?id =查询字符串参数,所以我找到了2个可能的解决方案.
解决方案1
创建一个html帮助器来生成url.任何有关此代码的改进意见将不胜感激.
public static MvcHtmlString ActionLinkSafeId(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
{
var routeValuesDictionary = new RouteValueDictionary(routeValues);
MvcHtmlString link = htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
if (routeValuesDictionary.ContainsKey("id") && routeValuesDictionary["id"].ToString().Contains('.'))
{
if (routeValuesDictionary.Count == 1)
{
var searchValue = …Run Code Online (Sandbox Code Playgroud)