简体   繁体   中英

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. I use a create-react-app project for the front end.

When I debug locally and communicate with.Net Core WEB API, it works fine:

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.

But when I testing backend APIs by fetching get on

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

I got 404 .

My IIS configuration: 我的 IIS 配置:

The web.config on.Net Core Web API build folder:

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

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

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": {
        "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:

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

You can try to use http access first, if http can be accessed successfully then this should be a certificate verification issue. If 404 still appears when using http, then.Net Core Web API should not be successfully deployed to IIS. It could also be because you used the wrong 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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