简体   繁体   English

将 .NET Core Web API 部署到 IIS 服务器

[英]Deploying .NET Core Web API to IIS Server

I created my first.Net Core Web API and trying to deploy it to a remote IIS server.我创建了我的第一个.Net Core Web API 并尝试将其部署到远程 IIS 服务器。 I use a create-react-app project for the front end.我在前端使用了一个create-react-app项目。

When I debug locally and communicate with.Net Core WEB API, it works fine:当我在本地调试并与.Net Core WEB API 通信时,它工作正常:

https://localhost:5001/api/notes

I use https://myapp.eastus.cloudapp.azure.com as my public URL (here myapp is an example), and everything looks fine on browser that the front end pages load correctly. I use https://myapp.eastus.cloudapp.azure.com as my public URL (here myapp is an example), and everything looks fine on browser that the front end pages load correctly.

But when I testing backend APIs by fetching get on但是当我通过获取get测试后端 API 时

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

I got 404 .我得到了404

My IIS configuration:我的 IIS 配置: 我的 IIS 配置:

The web.config on.Net Core Web API build folder: web.config 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>

And React web.config file:并反应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>

UPDATED :更新

My DbContext EF Core instance:我的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"));
        }
    }
}

And, appsettings.json in.Net Core Project:并且, appsettings.json in.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'
    }
}

My Controller code:我的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());
        }

    }
}

What I did wrong?我做错了什么? How to debug this?如何调试这个?

If you use HTTPS (using TLS for authentication), you will not receive any response if TLS fails.如果您使用 HTTPS(使用 TLS 进行身份验证),如果 TLS 失败,您将不会收到任何响应。 Therefore, the cause of this problem may be that the site you want to visit does not support HTTPS, or the URL does not exist.所以出现这个问题的原因可能是你要访问的站点不支持HTTPS,或者URL不存在。

You can try to use http access first, if http can be accessed successfully then this should be a certificate verification issue.可以先尝试使用http访问,如果http可以访问成功则应该是证书验证问题。 If 404 still appears when using http, then.Net Core Web API should not be successfully deployed to IIS.如果使用http时仍然出现404,那么.Net Core Web API应该没有成功部署到Z5E265ACF46064EFB27. It could also be because you used the wrong URL.也可能是因为您使用了错误的 URL。 For information about "Host ASP.NET Core on Windows with IIS", you can refer to this link: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-5.0关于“Host ASP.NET Core on Windows with IIS”的信息,可以参考这个链接: https://docs.microsoft-aspnetcored/en-us/aspnet/? -5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM