c# - 将 .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。
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>
更新:
我的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"));
}
}
}
而且,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());
}
}
}
我做错了什么?如何调试这个?
解决方案
如果您使用 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
推荐阅读
- php - Zend MySQL 检查表是否存在
- java - 手动锁定手机后是否可以不重新创建活动?
- laravel - Fedora 中 Docker 中的卷权限被拒绝
- facebook-graph-api - 通过 facebook/instagram api 为我自己的用户获取最新图像?
- javascript - 如何在 cypress 中测试一个错误的请求
- facebook-graph-api - Facebook 营销 Adindsight API 中的“展示位置”字段
- javascript - Three.js 输出中的缓慢渲染过程
- postgresql - Sqlalchemy json 数组,在数组内执行类似查询
- python-2.7 - 离线安装 Python 2.7.16 和软件包。依赖关系的担忧
- ruby-on-rails - 没有路线匹配 [POST] "/line_items/1"