【问题标题】:Deploying .NET Core Web API to IIS Server将 .NET Core Web API 部署到 IIS 服务器
【发布时间】:2021-03-22 10:32:45
【问题描述】:

我创建了我的第一个 .Net Core Web API 并尝试将其部署到远程 IIS 服务器。我在前端使用create-react-app 项目。

当我在本地调试并与 .Net Core WEB API 通信时,它工作正常:

https://localhost:5001/api/notes

我使用https://myapp.eastus.cloudapp.azure.com 作为我的公共 URL(这里 myapp 是一个示例),在前端页面正确加载的浏览器上一切正常。

但是当我通过获取 get on 来测试后端 API 时

https://myapp.eastus.cloudapp.azure.com/api/notes

我得到了 404

我的 IIS 配置:

.Net Core Web API 构建文件夹上的web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\myapp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>

  </location>

</configuration>

然后反应web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <remove fileExtension=".js" />
            <mimeMap fileExtension=".js" mimeType="application/javascript; charset=UTF-8" />
        </staticContent>
        <rewrite>
            <rules>
                
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

更新

我的DbContext EF Core 实例:

using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;

namespace Notes.DB
{
    public class AppDbContext : DbContext
    {
        protected readonly IConfiguration Configuration;
        public DbSet<Note> Notes { get; set; }

        public AppDbContext(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
               Configuration.GetConnectionString("myDb1"));
        }
    }
}

还有,.Net Core 项目中的appsettings.json

{
    "AppSettings": {
        "Secret": ""
    },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*",
    "ConnectionStrings": {
        "myDb1": 'I generated this string from Azure SQL Database'
    }
}

我的Controller 代码:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Notes.Core;
using Notes.DB;

namespace myApp.Controllers
{
    [ApiController]
    [Route("[controller]")]
    [Bind("Id,Username,Value")]

    public class NotesController : ControllerBase
    {

        private readonly ILogger<NotesController> _logger;
        private INotesServices _notesSerives;
        private readonly AppDbContext _context;

        public NotesController(ILogger<NotesController> logger, INotesServices notesSerives, AppDbContext context)
        {
            _logger = logger;
            _notesSerives = notesSerives;
            _context = context;
        }

        [HttpGet("", Name = "GetAllNote")]
        public IActionResult GetAllNote()
        {
            return Ok(_notesSerives.GetAllNote());
        }

    }
}

我做错了什么?如何调试?

【问题讨论】:

  • 根据您分享的有关 IIS 配置的有限信息,正确的 URL 可能是 https://myapp.eastus.cloudapp.azure.com/api/notes
  • @LexLi 使用 https://myapp.eastus.cloudapp.azure.com/api/notes 给我一个 200 但返回一个 HTML 文档,所以我猜 URL 仍然是错误的?
  • 您好,问题解决了吗?是否可以使用 http 访问?

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


【解决方案1】:

如果您使用 HTTPS(使用 TLS 进行身份验证),如果 TLS 失败,您将不会收到任何响应。因此,出现此问题的原因可能是您要访问的站点不支持HTTPS,或者该URL不存在。

您可以先尝试使用http访问,如果http可以访问成功则应该是证书验证问题。如果使用 http 时仍然出现 404,那么 .Net Core Web API 应该没有成功部署到 IIS。也可能是因为您使用了错误的 URL。关于“使用 IIS 在 Windows 上托管 ASP.NET Core”的信息,可以参考这个链接:https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-5.0

【讨论】:

  • 您好,按照您的指示,我意识到问题可能是 .Net Core API 后端部署不成功。我现在可以正确运行 https,但我收到 500 - 内部服务器错误。所以我猜config 文件出了点​​问题?
  • 你安装了 ASP.NET Core Module/Hosting Bundle 吗?
  • 是的,我做到了。我不小心安装了错误版本的 Bundle 5.0。然后我在服务器上将其删除并下载了 .Net Core 3.1.0,这是我的 Web API 项目的版本。重新启动 IIS 后,我可以从浏览器中看到 500 仍然存在,这次“没有可用的响应数据”。
  • IIS 能否成功托管核心 MVC 项目?我必须确认这个问题是否与 IIS 或 Web api 项目有关。
  • 您必须确保 IIS 能够成功托管核心项目。
猜你喜欢
  • 1970-01-01
  • 2020-04-15
  • 2017-02-13
  • 2019-11-05
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
相关资源
最近更新 更多