简体   繁体   中英

MVC3 - How to handle url string which can add end of with from user input ( browser addressbar )

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...

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM