简体   繁体   English

如何处理MVC5中文件的404错误?

[英]How to handle 404 error for files in MVC5?

I wrote Controller to handle 404-page error for my MVC5 application as it was showed here . 我写的控制器来处理404页的错误我MVC5的应用程序,因为它是显示在这里 However it works only with HTML-pages that has controller and method. 但是,它仅适用于具有控制器和方法的HTML页面。

It doesn't work with files\\images that just has a link inside the site. 它不适用于仅在站点内部具有链接的文件\\图像。 For example Sample.chtml: 例如Sample.chtml:

    <a href="@material.FilePath"><i class="material-icons left">file_download</i>@material.Title</a>`

where FilePath is just reference to ~Content/images/... or ~/Content/Files/file.pdf. 其中FilePath仅引用〜Content / images / ...或〜/ Content / Files / file.pdf。

When I try to go through this links I get standard 404 NotFound page and Error controller or Application_Error from Global.asax.cs doesn't call. 当我尝试通过此链接时,会看到标准的404 NotFound页面,并且不会调用Global.asax.cs中的Error控制器或Application_Error。

Especially I need this handler for file-not-found case. 特别是对于文件未找到的情况,我需要此处理程序。 How can I solve this problem? 我怎么解决这个问题?

Edited : Important moment : this files has just pure-html-link and no controller\\method defined. 编辑:重要时刻:该文件仅具有纯HTML链接,没有定义controller \\ method。

Search NuGet for the MvcNotFound package. 在NuGet中搜索MvcNotFound软件包。 It handles all of this very neatly for you. 它为您非常巧妙地处理了所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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