【发布时间】: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。
.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