参数中带有斜杠的 URL?
问题:
我正在创建一个 wiki 软件,基本上是 wikipedia/mediawiki 的克隆,但是使用 ASP.NET MVC(MVC 是重点,所以不推荐我 ScrewTurn)。
现在我有一个问题:
我使用此路由映射来路由如下 URL:
http://en.wikipedia.org/wiki/ASP.NET
routes.MapRoute(
"Wiki", // Routenname
//"{controller}/{action}/{id}", // URL mit Parametern
"wiki/{id}", // URL mit Parametern
new { controller = "Wiki", action = "dbLookup", id = UrlParameter.Optional } // Parameterstandardwerte
);
现在我突然想到,有可能是“AS/400”之类的标题:
http://en.wikipedia.org/wiki/AS/400
顺便说一句,还有这个(标题'斜线'):
http://en.wikipedia.org/wiki//
还有这个:
http://en.wikipedia.org/wiki//dev/null
总的来说,维基百科似乎有一个有趣的标题列表如下: http://en.wikipedia.org/wiki/Wikipedia:Articles_with_slashes_in_title
如何正确制作像这样的路线?
编辑:
类似于:
如果 URL 以 /Wiki/ 开头,并且不以 /wiki/Edit/ 开头 (但不是/维基/编辑) 然后将 URL 的其余部分作为 Id 传递。
编辑:
嗯,还有一个问题: 我该如何路由这个:
http://en.wikipedia.org/wiki/C&A
维基百科可以...
编辑:
根据维基百科,由于与维基文本语法冲突,只有以下字符永远不能在页面标题中使用(DISPLAYTITLE 也不支持它们):
# < > [ ] | { }
<强>编辑:
要允许 * 和 &,请放入
<httpRuntime requestPathInvalidCharacters="" />
(在这里找到:http://www.christophercrooker.com/use-any-characters-you-want-in-your-urls-with-aspnet-4-and-iis)
Question:
I am creating a wiki software, basically a clone of wikipedia/mediawiki, but in ASP.NET MVC (the MVC is the point, so don't recommend me ScrewTurn).
Now I have a question:
I use this route mapping, to route a URL like:
http://en.wikipedia.org/wiki/ASP.NET
routes.MapRoute(
"Wiki", // Routenname
//"{controller}/{action}/{id}", // URL mit Parametern
"wiki/{id}", // URL mit Parametern
new { controller = "Wiki", action = "dbLookup", id = UrlParameter.Optional } // Parameterstandardwerte
);
Now it just occured to me, that there might be titles like 'AS/400':
http://en.wikipedia.org/wiki/AS/400
Incidentially, there is also this one (title 'Slash'):
http://en.wikipedia.org/wiki//
And this one:
http://en.wikipedia.org/wiki//dev/null
Overall, Wikipedia seems to have a list of interesting titles like this:
http://en.wikipedia.org/wiki/Wikipedia:Articles_with_slashes_in_title
How do I make routes like this route correctly ?
Edit:
Something like:
If the URL starts with /Wiki/, and if it doesn't start with /wiki/Edit/
(but not /Wiki/Edit)
then pass all the rest of the URL as Id.
Edit:
Hmm, just another problem:
How can I route this one:
http://en.wikipedia.org/wiki/C&A
Wikipedia can...
Edit:
According to wikipedia, due to clashes with wikitext syntax, only the following characters can never be used in page titles (nor are they supported by DISPLAYTITLE):
# < > [ ] | { }
Edit:
To allow * and &, put
<httpRuntime requestPathInvalidCharacters="" />
into section <system.web> in file web.config
(Found here: http://www.christophercrooker.com/use-any-characters-you-want-in-your-urls-with-aspnet-4-and-iis)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用包罗万象的路由将网址的
wiki
部分后面的所有内容捕获到id
令牌中:现在,如果您有以下请求:
/wiki/ AS/400
它将映射到Wiki
控制器上的以下操作:就
/wiki//
而言,我相信您会得到 400 Bad在该请求到达之前,来自 Web 服务器的请求错误ASP.NET 管道。您可以查看以下博客文章。You could use a catchall route to capture everything that follows the
wiki
part of the url into theid
token:Now if you have the following request:
/wiki/AS/400
it will map to the following action on theWiki
controller:As far as
/wiki//
is concerned I believe you will get a 400 Bad Request error from the web server before this request ever reaches the ASP.NET pipeline. You may checkout the following blog post.在mvc中的属性路由中,我在
HttpGet
中的字符串abc/cde
中遇到了同样的问题,所以你有放置
*
因为在此之后它将被视为参数in
Attribute Routing
in mvc i had the same problem having/
in stringabc/cde
inHttpGet
so you have to place
*
because after this it will be considered as parameterQuandry - 也许您已经弄清楚了这一点,因为您的问题已经存在一年多了,但是当您调用 RedirectToAction,您实际上是在向浏览器发送 HTTP 302 响应,这会导致浏览器向指定的操作发出 GET 请求。因此,您看到的是无限循环。
请参阅:Controller.RedirectToAction 方法
Quandry - maybe you have already figured this out since your question is over a year old, but when you call RedirectToAction, you are actually sending an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action. Hence, the infinite loop you are seeing.
See: Controller.RedirectToAction Method
仍然作为一个选项在文件 Global.asax 中写入:
Still as an option write in the file Global.asax: