ASP.NET Core发布到IIS 7.5时出现404错误

4
我正在使用Visual Studio 2015将我的ASP.NET Core应用程序发布到IIS 7.5。我想要做的就是在wwwroot中查看一个普通的default.htm页面。当我使用VS的IIS Express时,一切都正常,但是当我将其发布到IIS 7.5并将物理路径指向Visual Studio在发布时创建的wwwroot文件夹时,我什么也看不到,只有一个空白屏幕(404)。奇怪的是,当我从startup.cs的Configure方法中运行默认的app.run方法时,它完美地工作:
app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });

然而,当我注释掉这部分代码,使用app.UseDefaultFiles()和app.UseStaticFiles()时,我什么也看不到。以下是我的Startup.cs文件:

public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();
            //app.UseDirectoryBrowser();
            //app.UseDefaultFiles();
            //app.UseStaticFiles();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }

这是我的web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
  </system.webServer>
</configuration>

以下是我的project.json文件:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final"
  },

  "commands": {
    "web": "Microsoft.AspNet.Server.Kestrel"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}

我已经确认我已经下载了httpPlatformHandler v1.2,当我从VS发布时,我针对DNX版本dnx-clr-winx86.1.0.0-rc1-update1进行目标设置,并勾选以下两个选项(在发布之前删除所有现有文件和将源文件编译为NuGet包)。它在IIS Express中运行得很好。当我尝试使用IIS 7.5时,它开始变得奇怪。
有什么建议吗?

你尝试过在你的wwwroot中放置一个index.html文件吗?如果你没有配置路由,并且没有使用app.Run返回任何内容,那么导航到站点的根目录将会导致404错误,因为...嗯...找不到文件。 - Will Ray
@Will 是的。我在wwwroot文件夹中有一个index.html和一个web.config。 - Brandon
我曾经遇到过同样的问题,但无法解决它... - IrishChieftain
1
@IrishChieftain,我已经在上面给出了解决方案。希望能对你有所帮助! - Brandon
不错,Brandon,非常感谢! :) - IrishChieftain
1个回答

3

编辑 - 我解决了这个问题

出于某种原因,在您的Configure方法之前,必须拥有一个Configure1方法。在Configure方法中,您必须使用app.map()方法。此外,我注意到您必须在IIS中创建一个新站点,并将应用程序发布到该文件夹,同时设置物理路径为wwwroot。站点名称和app.map()名称必须匹配。请参见下面的示例:

        public Startup(IHostingEnvironment env)
        {
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure1(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseIISPlatformHandler();
            app.UseDefaultFiles();
            app.UseStaticFiles();
            app.UseMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.Map("/appRoot", (app1) => this.Configure1(app1, env, loggerFactory));
        }

        // Entry point for the application.
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);

现在,我遇到了我的WebAPI出现问题的情况。希望这可以帮助!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接