.Net Core如何调试路由404问题
.Net Core How debug routing 404 issues
我有一个 .net 核心 web api 应用程序,我最近尝试在一台新计算机上调试(在 VS2017 中)(在长期中断使用该应用程序之后)。我还将 .net 核心从 1.1 升级到 2.2。但是,它似乎不再路由 ,甚至路由到我出于测试目的而留在其中的 ValuesController,也不只是使用本地主机(我一直在用我的本地 ip 地址测试它) .我在同一个解决方案中创建了另一个 web api 项目(路由很好)并比较了代码、配置文件等;我已经简化了我自己的代码以极端匹配工作网络 api 代码:
public class Program {
public static void Main(string[] args) {
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
我已经去掉了 Startup.cs
的所有内容
public partial class Startup {
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
...
并确保我的 launchSettings.json 也与我的测试网站 api 匹配(已尝试直接启动这两种服务,或通过 IISExpress)。直接启动的相关位:
"profiles": {
"MyWebApiService": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:8080;https://localhost:44385"
}
}
但是当它启动时,虽然它首先调用 http://localhost:8080/api/values, and the redirect to https://localhost:44385/api/values works fine, the call to https://localhost:44385/api/values returns http 代码 404。
有什么方法可以查看路由中的情况吗?它在寻找什么以及在哪里?
在您提供的代码中,我没有在 ConfigureServices
中看到 AddHttpsRedirection
。请添加:
services.AddHttpsRedirection(options =>
{
options.HttpsPort = 443;
});
与类似的问题,但我没有检查已发布的版本。此外,我在 .csproj 文件的 节点中设置的 Sdk 是:
- Microsoft.NET.Sdk.Web
理应如此。但是,我确实在那里设置了
并明确设置为 false,这在 v.2.1 之前似乎运行良好。将其设置为 true 是这里的解决方案:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
...
"MVC 2.1.0 requires compilation context to be available. Compilation context tells it if a library references MVC which is used as a filter to skip assemblies that are deemed unlikely to have controllers." 参见 ASP.NET Core 2.1.0 breaks attribute-routed endpoints in applications targeting Microsoft.NET.Sdk #8388
请注意,此问题存在于所有 Microsoft.AspNetCore.App 和 Microsoft.AspNetCore.All 版本 2.1.0、2.1.1、2.1.2 和 2.1.3 中。
我有一个 .net 核心 web api 应用程序,我最近尝试在一台新计算机上调试(在 VS2017 中)(在长期中断使用该应用程序之后)。我还将 .net 核心从 1.1 升级到 2.2。但是,它似乎不再路由 ,甚至路由到我出于测试目的而留在其中的 ValuesController,也不只是使用本地主机(我一直在用我的本地 ip 地址测试它) .我在同一个解决方案中创建了另一个 web api 项目(路由很好)并比较了代码、配置文件等;我已经简化了我自己的代码以极端匹配工作网络 api 代码:
public class Program {
public static void Main(string[] args) {
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
我已经去掉了 Startup.cs
的所有内容public partial class Startup {
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) {
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
} else {
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
...
并确保我的 launchSettings.json 也与我的测试网站 api 匹配(已尝试直接启动这两种服务,或通过 IISExpress)。直接启动的相关位:
"profiles": {
"MyWebApiService": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:8080;https://localhost:44385"
}
}
但是当它启动时,虽然它首先调用 http://localhost:8080/api/values, and the redirect to https://localhost:44385/api/values works fine, the call to https://localhost:44385/api/values returns http 代码 404。
有什么方法可以查看路由中的情况吗?它在寻找什么以及在哪里?
在您提供的代码中,我没有在 ConfigureServices
中看到 AddHttpsRedirection
。请添加:
services.AddHttpsRedirection(options =>
{
options.HttpsPort = 443;
});
与
- Microsoft.NET.Sdk.Web
理应如此。但是,我确实在那里设置了
并明确设置为 false,这在 v.2.1 之前似乎运行良好。将其设置为 true 是这里的解决方案:<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.2</TargetFramework> <PreserveCompilationContext>true</PreserveCompilationContext> ...
"MVC 2.1.0 requires compilation context to be available. Compilation context tells it if a library references MVC which is used as a filter to skip assemblies that are deemed unlikely to have controllers." 参见 ASP.NET Core 2.1.0 breaks attribute-routed endpoints in applications targeting Microsoft.NET.Sdk #8388
请注意,此问题存在于所有 Microsoft.AspNetCore.App 和 Microsoft.AspNetCore.All 版本 2.1.0、2.1.1、2.1.2 和 2.1.3 中。