[英]How to forward one url to 404-error page on MVC
我的mvc應用程序正在使用以下網址:
/Content/ /Content/style.css /Content/image.png
我想為URL /Content/
創建轉發,應該轉發給404錯誤。 所有其他網址都可以使用。 怎么做?
我嘗試了以下解決方案,但是它將所有文件從/ Content /轉發到404錯誤頁面,但是我只希望主目錄被阻止。
Web.config:
<validation validateIntegratedModeConfiguration="false" />
<handlers>
...
<add name="hideDirectoryContent" verb="*" path="Content" type=" NameSspace.NoAccessHandler"/>
</handlers>
</system.webServer>
NoAccesHandler.cs:
public class NoAccessHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.StatusCode = 404;
}
#endregion
}
為此,您只需要創建一個如下所示的控制器(在mvc 4上進行了測試)
public class ContentController : Controller
{
// GET: Content
[AllowAnonymous]
public ActionResult Index()
{
return new HttpStatusCodeResult(HttpStatusCode.NotFound, "Not Found");
}
}
並在您的RoutesCollection中使用
routes.RouteExistingFiles = true;
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.