首页 > 解决方案 > 将 .NET Core Web API 部署到 IIS 服务器

问题描述

我创建了我的第一个 .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是一个示例),并且前端页面正确加载的浏览器上的一切看起来都很好。

但是当我通过获取来测试后端 APIget

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

我得到了404

我的 IIS 配置: 我的 IIS 配置:

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

<?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>

更新

我的DbContextEF 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"));
        }
    }
}

而且,appsettings.json在 .Net Core 项目中:

{
    "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());
        }

    }
}

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

标签: c#reactjsasp.net-coreiisasp.net-core-webapi

解决方案


如果您使用 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


推荐阅读