C#.NET MVC- HTTP 错误 403.14 - 禁止 Web 服务器配置为不列出此目录的内容

C#.NET MVC- HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory

MVC项目根目录下有Startup.cs

我启用了目录浏览。但是在 运行 上,项目显示以下屏幕。我希望在 MVC 项目中,不需要设置默认页面。我还尝试在默认文档 (IIS) 中添加 Startup.cs,但它会抛出扩展名被拒绝的错误。尝试在 ApplicationHost.config 文件中添加 .cs 扩展名。但它给出了更复杂的错误。

Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <defaultDocument enabled="true" />
        <directoryBrowse showFlags="Date, Time, Size, Extension, LongDate" />
  </system.webServer>  
</configuration>

HTTP Error 403.14 - Forbidden

Directory Listing and IIS Directory browsing enabled

Error after setting default document to Startup.cs

这是在asp.net

中启用目录浏览的完整配置
https://docs.microsoft.com/en-us/iis/configuration/system.webserver/directorybrowse

您不需要目录浏览来显示 ASP.NET MVC 站点。

但看起来您的网站很旧 "server"。您必须做的一件事是安装 DotNetCore.2.0.0-WindowsHosting.exe(看起来您制作了一个 ASP.NET Core MVC 站点)。 看本页结尾:https://github.com/dotnet/core/blob/master/release-notes/download-archives/2.0.0-download.md

web.config 还必须包含所需的程序集。

编辑:

再次查看您的屏幕截图后,您似乎已将项目中的所有源代码添加到 Web 服务器。这不是它的工作原理。 您必须编译 ASP.NET 项目并将其发布到 Web 服务器(只需复制编译后的输出即可完成)。

我建议寻找 ASP.NET MVC 教程以掌握基础知识。并可能尝试使用较新版本的 IIS =D.

我刚刚发现这似乎显示了基础知识: https://www.tutlane.com/tutorial/aspnet-mvc/asp-net-mvc-publish-with-file-system

在用于服务文件的 IIS 中如果文件是脚本,则应添加处理程序,但如果要下载文件,则必须为其添加 MIME 映射。请注意,要访问任何文件,您应该启用其扩展名(此处为 .cs),然后在 URL 中引用带有扩展名的确切文件名。似乎您的 URL 不包含您的文件名。还要注意您的文件不要位于 IIS 配置中的隐藏段中。

并且如果您使用的是 asp.net 核心部分本机 IIS 模块和所有 IIS 托管模块无法处理对 ASP.NET 核心应用程序的请求。看这里 (https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/modules?view=aspnetcore-3.0).

此外,对于访问被阻止的文件,您不需要启用目录浏览。 该文档解释了 iis 中的文件扩展: https://docs.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/fileextensions/

您的案例将适用于此配置:

 <system.webServer>
 <staticContent>
        <mimeMap fileExtension=".cs" mimeType="text/plain" />
 </staticContent>
 <security>
    <requestFiltering>
        <fileExtensions>
                <remove fileExtension=".cs" />
                <add fileExtension=".cs" allowed="true" />
          </fileExtensions>
     </requestFiltering>
 </security>
</system.webServer>