我认为问题很清楚。我想处理可由用户添加的 url 字符串。
例子;
http://download.cnet.com/windows/script alert('hello') /script
或者
http://download.cnet.com/windows/aaaaaaaaaaaaa
正如您在示例中看到的,cnet 处理这些输入并将用户重定向到自定义 404 文件。
我正在研究 mvc3 剃须刀,我想它是带有控制器的东西,但我做不到。
额外信息:
我想做的事;我想要处理可以添加到 url 末尾的外部字符串。
另一个例子; http://www.yazilimdevi.com/yazilimdevi/aaaaaaaaa
如您所见,如果用户在网址末尾输入“aaaaaa”;他/她看到由 IIS 准备的“应用程序中的服务器错误”。我想创建一个自定义页面,并重定向所有添加未知路径、字符串或脚本的用户...
谢谢...
I think question is clear. I want to handle url strings which can be added by user.
Example;
http://download.cnet.com/windows/script alert('hello') /script
or
http://download.cnet.com/windows/aaaaaaaaaaaaa
As you can see in examples, cnet handle these inputs and redirect user to custom 404 file.
I'm working on mvc3 razor, it's something with controller I suppose, but I can't make it.
Extra Information:
What I want to do; I want to handle or external string which can be added end of the url.
Another example; http://www.yazilimdevi.com/yazilimdevi/aaaaaaaaa
As you can see, if user input "aaaaaa" to end of url; he/she see "Server Error in Application" which was prepared by IIS. I want to create a custom page, and redirect all users who added unknown path, string or script...
Thanks...
发布评论
评论(1)
如果您想针对这种情况实施特殊处理,那么您可能需要使用
/{*任意_url_part}
方案添加新路由。例如,路由将匹配所有这些 url:
http://Server_url/constant_path/
(variable_part=="")http://Server_url/constant_path/aaaaaaaaaaaaa
(variable_part ==“aaaaaaaaaaaaa”)http://Server_url/constant_path/scriptalert('hello') /script
(variable_part=="scriptalert('hello') /script")等,无论用户输入多少斜杠或其他特殊符号。有关参考,请参阅 MSDN:ASP.NET 路由 -处理 URL 模式中可变数量的段
。
如果您不想为实现所有这些东西而烦恼,而只想向用户显示一个精美的 404 页面,那么您可以考虑使用标准 ASP.NET 功能 - 自定义错误页面
处理此类请求的其他策略也可以在这篇博文。
更新
如果您想采用第一种方式,您还必须添加控制器和视图来显示一些自定义错误页面。如果您采用与路线中相同的名称,则必须将以下内容添加到您的项目中:
File
Controllers/ErrorController.cs
contains classErrorController
with method显示错误
,如下所示:File < code>Views/Error/ShowError.aspx - 一个简单的 HTML 视图,用于显示错误信息,如下所示:
If you want to implement special handling for this scenario then you may want to add new route using
/{*arbitrary_url_part}
scheme. For example, routewill match all those urls:
http://Server_url/constant_path/
(variable_part=="")http://Server_url/constant_path/aaaaaaaaaaaaa
(variable_part=="aaaaaaaaaaaaa")http://Server_url/constant_path/script alert('hello') /script
(variable_part=="script alert('hello') /script")etc. no matter how much slashes or other special symbols user puts in. For reference, see MSDN: ASP.NET Routing - Handling a Variable Number of Segments in a URL Pattern
.
If you don't want to bother yourself with implementing all this stuff and only want to show a fancy 404 page to user then you might consider using standard ASP.NET feature - Custom error pages
Other strategies on handling such requests can also be found in This blog post.
UPDATE
If you want to go the first way, you'll also have to add controller and view for displaying some custom error page. If you take the same names as in route you'll have to add following to your project:
File
Controllers/ErrorController.cs
containing classErrorController
with methodShowError
, like this:File
Views/Error/ShowError.aspx
- a simple HTML view to display error information, like this: