WebApi在调试时提供404;发布时有效 [英] WebApi giving 404 whilst debugging; works when published
问题描述
我有一个用.NET Core 2.2.6编写的控制台应用程序,它使用Kestrel托管简单的WebApi。
公共类SettingsController:控制器
{
//
// GET:/ settings /
public string Index()
{
return $ Hello world!控制器;
}
}
如果我发布代码并运行可执行文件,我可以访问
其他一些可能有助于查明问题的代码:
公共静态IWebHostBuilder CreateWebHostBuilder(string [] args)=>
WebHost.CreateDefaultBuilder(args)
.ConfigureKestrel((context,options)=>
{
options.ListenAnyIP(310,listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1;
});
})
.UseStartup< Startup>();
公共类启动
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app,IHostingEnvironment env)
{
app.UseDefaultFiles(new DefaultFilesOptions()
{
DefaultFileNames = new List< string>(){ index.html}
});
//返回静态文件并结束管道。
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24;
ctx。 Context.Response.Headers [HeaderNames.CacheControl] =
public,max-age = + durationInSeconds;
}
});
//使用Cookie政策中间件来符合欧盟通用数据
//保护法规(GDPR)法规。
app.UseCookiePolicy();
//将MVC添加到请求管道。
app.UseMvcWithDefaultRoute();
}
}
一个非常相关的 GitHub问题,它解释了这里的情况。来自ASP.NET Core团队的Pranav K说:
MVC 2.1.0要求编译上下文可用。编译上下文告诉它库是否引用MVC,该MVC用作过滤器以跳过被认为不太可能具有控制器的程序集。 Microsoft.NET.Sdk没有设置
< PreserveCompilationContext> true< / PreserveCompilationContext>
,这可以解释您为什么看到此消息。
这意味着您可以使用几种可行的解决方案来解决您遇到的问题:
- 将
PreserveCompilationContext
属性添加到您的.csproj文件中,其值为true
,如上所示。 - 引用
Microsoft.NET.Sdk.Web
项目SDK,而不是Microsoft.NET.Sdk
。
我不知道这两个选项之间有什么明显的区别,但是我会更新项目SDK实际上是您正在构建的Web项目。
I have a console application written in .NET Core 2.2.6 that is using Kestrel to host a simple WebApi.
public class SettingsController : Controller
{
//
// GET: /settings/
public string Index()
{
return $"Hello world! controller";
}
}
If I publish the code and run the executable, I can visit http://127.0.0.1:310/settings and see the expected "Hello world! controller". However, if I debug (or even open in Release mode) from inside Visual Studio 2019, the same URL throws a 404 exception.
Some other code that might help pinpoint the problem:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureKestrel((context, options) =>
{
options.ListenAnyIP(310, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1;
});
})
.UseStartup<Startup>();
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDefaultFiles(new DefaultFilesOptions()
{
DefaultFileNames = new List<string>() { "index.html" }
});
// Return static files and end the pipeline.
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24;
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public,max-age=" + durationInSeconds;
}
});
// Use Cookie Policy Middleware to conform to EU General Data
// Protection Regulation (GDPR) regulations.
app.UseCookiePolicy();
// Add MVC to the request pipeline.
app.UseMvcWithDefaultRoute();
}
}
There's a very relevant GitHub issue that explains what's going on here. Pranav K from the ASP.NET Core team says:
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. Microsoft.NET.Sdk does not set
<PreserveCompilationContext>true</PreserveCompilationContext>
which would explain why you're seeing this.
This means there's a couple of working solutions to the problem you're seeing:
- Add the
PreserveCompilationContext
property to your .csproj file with a value oftrue
, as shown above. - Reference the
Microsoft.NET.Sdk.Web
project SDK instead ofMicrosoft.NET.Sdk
.
I don't know of any perceivable difference between these two options, but I would just update the project SDK given that it is in fact a Web project you're building.
这篇关于WebApi在调试时提供404;发布时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!