【问题标题】:Cant get ASP.NET Core Web API to work with IIS [closed]无法让 ASP.NET Core Web API 与 IIS 一起使用 [关闭]
【发布时间】:2017-01-22 01:23:11
【问题描述】:

我正在构建一个 ASP.NET Core MVC Web Api 应用程序,并试图让它在我自己的机器上与 IIS 一起工作。我已经阅读了不同的博客并尝试了不同的东西,但似乎无法让它工作......我的 Winform 客户端在调用 Web api 时只得到 404。通过网络浏览器导航到站点 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"
    }
  }
}

project.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 中的网站

IIS 中的应用程序池

网站路径的文件夹结构

我的 IIS

【问题讨论】:

  • 尝试指向 inetpub/wwwroot/mywebapi 而不是 inetpub/wwwroot/mywebapi/wwwroot
  • MyWebAPI 和 www 目录的安全设置是什么?
  • 对根文件夹 wwwroot 和子文件夹的 IUSR 和 IIS_IUSERS 的完全访问权限。更改到 inetpub/wwwroot/mywebapi 的路径没有区别。在某个地方阅读它必须指向已发布文件夹中的 wwwroot 文件夹。
  • 好的,所以我让它工作了......但我不知道为什么!简而言之编程 :) 我创建了一个新的 .net Core Web 项目并使用 IIS 对其进行了测试。它工作得很好,所以我添加了更多..然后更多等等,直到我将所有内容从旧解决方案复制到新解决方案。现在它可以工作了!
  • 干得好。您最终指向的是 MyWebAPI 还是 wwwroot?

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


【解决方案1】:

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

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

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

【讨论】:

  • 试一试,但它不起作用。那么肯定有其他问题。
  • 您现在收到什么错误?我在本地使用了您的确切设置(除了您的 web.config 文件),并且能够在 IIS 中运行该站点。
  • {"响应状态码不表示成功:502 (Bad Gateway)。"}
  • 啊哈。不同的错误。 :) 这就是进步。
  • @ChristianThoressonDahl 我已经能够使用您的 web.config 文件并使其也在本地工作。继续指向MyWebAPI 而不是wwwroot;检查您的 .\logs\aspnetcore-stdout 文件和事件查看器中的 IIS 日志。试试我为您发布的 GitHub 演示,作为最小复制。祝你好运。
猜你喜欢
  • 1970-01-01
  • 2016-01-06
  • 2018-10-26
  • 1970-01-01
  • 2018-04-02
  • 1970-01-01
  • 2019-03-04
  • 2012-08-20
  • 1970-01-01
相关资源
最近更新 更多