asp.net-mvc 你如何在 ASP.NET MVC 中的 ~/Views 文件夹下请求静态 .html 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17949460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:03:34  来源:igfitidea点击:

How do you request static .html files under the ~/Views folder in ASP.NET MVC?

asp.net-mvcasp.net-mvc-routing

提问by alex.mironov

I want to be able to request static .htmlfiles which are located in the ~/Viewsfolder. According to the documentation, the routing system checks to see if a URL matches a disk file before evaluating the application's routes.

我希望能够请求.html位于~/Views文件夹中的静态文件。根据文档,路由系统在评估应用程序的路由之前会检查 URL 是否与磁盘文件匹配。

But when I request the file a 404error arises.

但是当我请求文件时出现404错误。

My file is located in ~/Views folder. The URL is: http://[localhost]/Views/HtmlPage1.html

我的文件位于 ~/Views 文件夹中。网址是:http://[localhost]/Views/HtmlPage1.html

What have I missed?

我错过了什么?

回答by Darin Dimitrov

I want to be able to request static .html files which are located in the '~/Views' folder.

我希望能够请求位于“~/Views”文件夹中的静态 .html 文件。

You can't. There's a web.config file in this folder which explicitly forbids accessing any file from it. If you want to be able to access files from the client those files should not be placed in the Viewsfolder which has a special meaning in ASP.NET MVC.

你不能。此文件夹中有一个 web.config 文件,它明确禁止从中访问任何文件。如果您希望能够从客户端访问文件,则不应将这些文件放在ViewsASP.NET MVC 中具有特殊含义的文件夹中。

You could have a ~/Staticfolder where you could place your HTML files. And then access it like that:

您可以有一个~/Static文件夹,您可以在其中放置 HTML 文件。然后像这样访问它:

http://example.com/yourapplicationname/static/foo.html

回答by cs3x

To allow files like js and html in Views folder edit the web.config in views-Folder:

要允许视图文件夹中的 js 和 html 等文件编辑视图文件夹中的 web.config:

<system.webServer>
<handlers>
  <add name="JavaScriptHandler" path="*.js" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />      
  <add name="HtmlScriptHandler" path="*.html" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

回答by Vikas

I want to be able to request static .html files which are located in the ~/Views folder.

我希望能够请求位于 ~/Views 文件夹中的静态 .html 文件。

Well you can. The marked answer is not entirely correct, although it gives a solution.

你可以。尽管给出了解决方案,但标记的答案并不完全正确。

The reasoning in the marked answer is correct, it is web.config (BlockViewHandler setting to be specific) in the Views folder that prevents the files to be accessed directly. It is there for securing the views in Asp.Net MVC. But if you asked a question about serving these files directly then you likely have a valid reason to do so, like using AngularJS partial views (as in our case) where we do not want to duplicate the views folder with weird names.

标记答案中的推理是正确的,它是 Views 文件夹中的 web.config (特定于 BlockViewHandler 设置)阻止直接访问文件。它用于保护 Asp.Net MVC 中的视图。但是如果你问了一个关于直接提供这些文件的问题,那么你可能有一个正当的理由这样做,比如使用 AngularJS 部分视图(如我们的例子),我们不想用奇怪的名字复制视图文件夹。

So here is a very simple tweak you can do in the web.config file found in the Views folder, without compromising security of your asp.net mvc views. This will secure the .cshtml files as usual but leave your .html files alone.A

所以这里有一个非常简单的调整,你可以在 Views 文件夹中的 web.config 文件中进行,而不会影响你的 asp.net mvc 视图的安全性。这将像往常一样保护 .cshtml 文件,但不要管你的 .html 文件。A

Change this

改变这个

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode"    type="System.Web.HttpNotFoundHandler" />

--to--

- 到 -

<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

回答by Venkatesh Muniyandi

Another alternate option is to insert an action method in any of the desired controller to serve the html file

另一种选择是在任何所需的控制器中插入一个动作方法来提供 html 文件

public ActionResult MyHtml()
{
    var result = new FilePathResult("~/Views/HtmlPage1.html", "text/html");
    return result;
}

Access the html as http://yoursite/controller/MyHtml. You may extend this action method to accept the html file name as method/querystrign parameter and render the file at run time, e.g something like this.

访问 html 作为http://yoursite/controller/MyHtml。您可以扩展此操作方法以接受 html 文件名作为 method/querystrign 参数并在运行时呈现文件,例如这样的东西。

 public ActionResult MyHtml(string htmlPageName)
 {
      var result = new FilePathResult($"~/Views/{htmlPageName}.html", "text/html");
      return result;
 }

回答by Kurkula

If you are planning to use inside view folder, above answers should be best but this answer may be useful for users who are migrating to asp.net mvc core. Placing files in wwwroot instead of views folder should make your html pages access easily as localhost/myfile.html

如果您打算使用内部视图文件夹,上面的答案应该是最好的,但这个答案可能对迁移到 asp.net mvc core 的用户有用。将文件放在 wwwroot 而不是 views 文件夹应该使您的 html 页面可以像 localhost/myfile.html 一样轻松访问