2016-10-21 4 views
0

に404エラーページに1つのURLを転送するために私のMVCアプリケーションはfolowing URLを使用している:どのようにMVC

/Content/ /Content/style.css /Content/image.png

私はそれが404エラーに転送されるURL /Content/のために前方に作成したいです。その他のURLはすべて動作するはずです。どうやってするの?

解決方法を試しましたが、/ 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 
    } 

答えて

0

あなただけの(MVC 4でテスト)以下のようなコントローラを作成する必要があり、これを行うに

public class ContentController : Controller 
{ 
    // GET: Content 
    [AllowAnonymous] 
    public ActionResult Index() 
    { 
     return new HttpStatusCodeResult(HttpStatusCode.NotFound, "Not Found"); 
    } 
} 

、あなたのRoutesCollectionでの使用

routes.RouteExistingFiles = true; 

これが役に立ちます

+0

'/ Content/file1.jpg 'のような他のパスがまだ動作している場合は、 –

関連する問題