[英]MVC3 - How to handle url string which can add end of with from user input ( browser addressbar )
我認為問題很明確。 我想處理可以由用戶添加的網址字符串。
例;
http://download.cnet.com/windows/script alert('hello') /script
要么
http://download.cnet.com/windows/aaaaaaaaaaaaa
如示例所示,cnet處理這些輸入並將用戶重定向到自定義404文件。
我正在研究mvc3剃須刀,我想它與控制器有關,但我做不到。
額外信息:我想做什么; 我想處理或可以在url末尾添加的外部字符串。 另一個例子; 如您所見,如果用戶在URL末尾輸入“ aaaaaa”,那么您會看到http://www.yazilimdevi.com/yazilimdevi/aaaaaaaaa 。 他/她看到IIS准備的“應用程序中的服務器錯誤”。 我想創建一個自定義頁面,並重定向添加了未知路徑,字符串或腳本的所有用戶...
謝謝...
如果要對此方案實施特殊處理,則可能要使用/{*arbitrary_url_part}
方案添加新路由。 例如,路線
routes.MapRoute(
"SampleRoute",
"constant_path/{*variable_part}",
new {controller = "ErrorController", action = "ShowError", variable_part = ""}
);
將匹配所有這些網址:
http://Server_url/constant_path/
(variable_part ==“”)
http://Server_url/constant_path/aaaaaaaaaaaaa
(variable_part ==“ aaaaaaaaaaaaaaa”)
http://Server_url/constant_path/script alert('hello') /script
(variable_part ==“ script警報('hello')/ script”)
等等。無論用戶輸入多少斜杠或其他特殊符號。有關參考,請參見MSDN:ASP.NET路由-處理URL模式中可變數量的段 。
如果您不想為實現所有這些東西而煩惱,而只想向用戶顯示精美的404頁面,則可以考慮使用標准ASP.NET功能- 自定義錯誤頁面
其他處理此類請求的策略也可以在此博客文章中找到。
更新
如果要采用第一種方法,則還必須添加控制器和視圖以顯示一些自定義錯誤頁面。 如果您使用與route中相同的名稱,則必須在項目中添加以下內容:
包含類ErrorController
和ShowError
方法的文件Controllers/ErrorController.cs
,如下所示:
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 } } }
File Views/Error/ShowError.aspx
顯示錯誤信息的簡單HTML視圖,如下所示:
<%@ 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.