c# - 无法让 ASP.NET Core Web API 与 IIS 一起工作

标签 c# iis asp.net-web-api .net-core asp.net-core-webapi

<分区>

我正在构建一个 ASP.NET Core MVC Web Api 应用程序,并试图让它在我自己的机器上与 IIS 一起工作。我已经阅读了不同的博客并尝试了不同的东西,但似乎无法让它工作......我的 Winform 客户端在调用 Web api 时只收到 404。通过 Web 浏览器导航到站点 roo 时出现 HTTP 错误 403.14 - 禁止访问。

我运行的是 Windows 10 专业版。已安装 IIS。安装了 ASP.NET Core 服务器托管包

我已经在 IIS 中添加了该网站。应用程序池设置为无托管代码。 在 VS2015 中,我将网站发布到本地文件夹。我将该文件夹的内容复制到 IIS 所在的网站文件夹中。然后我希望它能工作,但它没有:(

这是我的设置:

web.config

  <system.webServer>
<handlers>
  <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<directoryBrowse enabled="true"/>
<aspNetCore processPath="%LAUNCHER_PATH%"
      arguments="%LAUNCHER_ARGS%"
      stdoutLogEnabled="true"
      stdoutLogFile=".\logs\aspnetcore-stdout"
      forwardWindowsAuthToken="false" />

Program.cs

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseWebRoot("wwwroot")
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

StartUp.cs

        public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

项目.json:

    {
    "dependencies": {
    "Business": "1.0.0-*",
    "Data": "1.0.0-*",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0"
    },
    "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final"
      },
     "frameworks": {
     "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },
  "runtimes": {
    "win10-x64": {}
  },
  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

IIS 中的网站

enter image description here

IIS 中的应用程序池

enter image description here

网站路径的文件夹结构 enter image description here

我的 IIS

enter image description here

最佳答案

将 IIS 指向 MyWebAPI 而不是 MyWebAPI/wwwroot。否则您将收到“HTTP 错误 403.14 - 禁止访问”错误。

这是一个demo application on GitHub .它使用与您答案中的应用程序相同的设置。当我像这样将它发布到我的 IIS 时它起作用了:

dotnet publish -o C:\inetpub\wwwroot\sandbox\

关于c# - 无法让 ASP.NET Core Web API 与 IIS 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41786382/

相关文章:

c# - 无法插入到我的数据库表中

c# - 如何将方法名称传递给另一个方法并通过委托(delegate)变量调用它?

c# - ASP.NET 网站调试速度极慢,有数千个临时 ASP.NET 文件

angularjs - 在 Angular 的 $http 中使用 PUT 方法时向查询字符串添加参数

c# - 如何在同一项目中运行 IdentityServer 和 WebAPI

c# - 来自 [TestMethod] 的 Directory.CreateDirectory 行为异常

c# - 如何级联 SoftDelete?

c# - MsDeploy - 无法更新应用程序池

c# - ASP.NET MVC : How to redirect a non www to www and vice versa

c# - ObjectId 数组的 MongoDB C# BsonRepresentation