Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

c# - WebApi giving 404 whilst debugging; works when published

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.

Publish profile

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...