WebApi 在调试时给出 404;发表时有效 [英] WebApi giving 404 whilst debugging; works when published

查看:30
本文介绍了WebApi 在调试时给出 404;发表时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 .NET Core 2.2.6 编写的控制台应用程序,它使用 Kestrel 来托管一个简单的 WebApi.

公共类 SettingsController : 控制器{////获取:/设置/公共字符串索引(){return $"Hello world! controller";}}

如果我发布代码并运行可执行文件,我可以访问

其他一些可能有助于查明问题的代码:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>WebHost.CreateDefaultBuilder(args).ConfigureKestrel((context, options) =>{options.ListenAnyIP(310,listenOptions =>{listenOptions.Protocols = HttpProtocols.Http1;});}).UseStartup();公开课启动{public void ConfigureServices(IServiceCollection 服务){服务.AddMvc();}公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment 环境){app.UseDefaultFiles(new DefaultFilesOptions(){DefaultFileNames = new List() { "index.html" }});//返回静态文件并结束管道.app.UseStaticFiles(新的StaticFileOptions{OnPrepareResponse = ctx =>{const int durationInSeconds = 60 * 60 * 24;ctx.Context.Response.Headers[HeaderNames.CacheControl] =公共,最大年龄="+durationInSeconds;}});//使用 Cookie Policy Middleware 符合欧盟通用数据//保护条例 (GDPR) 条例.app.UseCookiePolicy();//将 MVC 添加到请求管道中.app.UseMvcWithDefaultRoute();}}

解决方案

有一个非常相关的 GitHub issue 这解释了这里发生了什么.来自 ASP.NET Core 团队的 Pranav K 说:

<块引用>

MVC 2.1.0 要求编译上下文可用.编译上下文告诉它一个库是否引用了 MVC,MVC 被用作过滤器来跳过被认为不太可能有控制器的程序集.Microsoft.NET.Sdk 没有设置 true</PreserveCompilationContext> 这可以解释为什么你会看到这个.

这意味着您看到的问题有几个可行的解决方案:

  1. PreserveCompilationContext 属性添加到您的 .csproj 文件中,值为 true,如上所示.
  2. 参考 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:

  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.

这篇关于WebApi 在调试时给出 404;发表时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆