MVC3 - 如何处理可以从用户输入(浏览器地址栏)添加结尾的 url 字符串

发布于 2024-12-26 19:36:12 字数 608 浏览 0 评论 0 原文

我认为问题很清楚。我想处理可由用户添加的 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧话新听 2025-01-02 19:36:12

如果您想针对这种情况实施特殊处理,那么您可能需要使用 /{*任意_url_part} 方案添加新路由。例如,路由

        routes.MapRoute(
            "SampleRoute",
            "constant_path/{*variable_part}",
            new {controller = "ErrorController", action = "ShowError", variable_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 功能 - 自定义错误页面

处理此类请求的其他策略也可以在这篇博文。

更新

如果您想采用第一种方式,您还必须添加控制器和视图来显示一些自定义错误页面。如果您采用与路线中相同的名称,则必须将以下内容添加到您的项目中:

  1. File Controllers/ErrorController.cs contains class ErrorController with method 显示错误,如下所示:

    使用 System.Web.Mvc;
    
    命名空间 Your_app_name_here.Controllers
    {
        公共类ErrorController:控制器
        {
            公共 ActionResult ShowError(字符串变量部分)
            {
                返回视图((对象)变量部分); // 这里需要转换为对象
            }
        }
    }
    
  2. File < code>Views/Error/ShowError.aspx - 一个简单的 HTML 视图,用于显示错误信息,如下所示:

    <%@ 页面标题="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    
    此处为错误页面标题
    <身体>
    

    variable_part = <%=Model.ToString()%>

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, route

        routes.MapRoute(
            "SampleRoute",
            "constant_path/{*variable_part}",
            new {controller = "ErrorController", action = "ShowError", variable_part = ""}
            );

will 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:

  1. File Controllers/ErrorController.cs containing class ErrorController with method ShowError, like this:

    using System.Web.Mvc;
    
    namespace Your_app_name_here.Controllers
    {
        public class ErrorController : Controller
        {
            public ActionResult ShowError(string variable_part)
            {
                return View((object)variable_part); // Cast to object is required here
            }
        }
    }
    
  2. File Views/Error/ShowError.aspx - a simple HTML view to display error information, like this:

    <%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
    <HTML>
    <HEAD><TITLE>Error page title here</TITLE></HEAD>
    <BODY>
    <H1>variable_part = <%=Model.ToString()%></H1>
    </BODY>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文