WebApi giving 404 whilst debugging; works when published

前端 未结 1 529
清酒与你
清酒与你 2020-12-06 20:39

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
{
    //          


        
相关标签:
1条回答
  • 2020-12-06 20:54

    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:

    1. Add the PreserveCompilationContext property to your .csproj file with a value of true, as shown above.
    2. Reference the Microsoft.NET.Sdk.Web project SDK instead of Microsoft.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.

    0 讨论(0)
提交回复
热议问题