参数中带斜杠的网址? [英] URLs with slash in parameter?
问题描述
问题:
我正在创建一个 wiki 软件,基本上是 wikipedia/mediawiki 的克隆,但在 ASP.NET MVC 中(MVC 是重点,所以不要推荐我螺丝旋转).
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).
现在我有一个问题:
我使用这个路由映射来路由一个像:
http://en.wikipedia.org/wiki/ASP.NET
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
);
现在我突然想到,可能有像AS/400"这样的标题:
http://en.wikipedia.org/wiki/AS/400
Now it just occured to me, that there might be titles like 'AS/400':
http://en.wikipedia.org/wiki/AS/400
顺便说一句,还有这个(标题'Slash'):
http://en.wikipedia.org/wiki//
Incidentially, there is also this one (title 'Slash'):
http://en.wikipedia.org/wiki//
还有这个:
http://en.wikipedia.org/wiki//dev/null
总的来说,维基百科似乎有一个有趣的标题列表,如下所示:http://en.wikipedia.org/wiki/Wikipedia:Articles_with_slashes_in_title
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 ?
类似于:
如果 URL 以/Wiki/开头,并且不以/wiki/Edit/开头(但不是/Wiki/Edit)然后将 URL 的所有其余部分作为 Id 传递.
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.
嗯,还有一个问题:我该如何路由这个:
http://en.wikipedia.org/wiki/C&A
维基百科可以...
根据维基百科,由于与 wikitext 语法的冲突,只有以下字符永远不能用于页面标题(DISPLAYTITLE 也不支持):
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):
# < > [ ] | { }
允许 * 和 &,把
To allow * and &, put
<httpRuntime requestPathInvalidCharacters="" />
进入
into section <system.web> in file web.config
(在这里找到:http://www.christophercrooker.com/use-any-characters-you-want-in-your-urls-with-aspnet-4-and-iis)
推荐答案
你可以使用一个包罗万象的路由来将 URL 的 wiki
部分后面的所有内容捕获到 id
令牌:
You could use a catchall route to capture everything that follows the wiki
part of the url into the id
token:
routes.MapRoute(
"Wiki",
"wiki/{*id}",
new { controller = "Wiki", action = "DbLookup", id = UrlParameter.Optional }
);
现在,如果您有以下请求:/wiki/AS/400
它将映射到 Wiki
控制器上的以下操作:
Now if you have the following request: /wiki/AS/400
it will map to the following action on the Wiki
controller:
public ActionResult DbLookup(string id)
{
// id will equal AS/400 here
...
}
就 /wiki//
而言,我相信在此请求到达 ASP.NET 管道之前,您将收到来自 Web 服务器的 400 Bad Request 错误.您可以查看以下博文.
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.
这篇关于参数中带斜杠的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!