UseMvcWithDefaultRoute

UseMvcWithDefaultRoute

本文介绍了路由不适用于自托管 Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上就是我所拥有的,一组非常简单的三个文件,带有新鲜的 asp.net core 2.1(实际上是从教程中复制粘贴的):

This is essentially what I have, a very simple set of three files with fresh asp.net core 2.1 (actually copy-pasted from tutorials):

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

然后是最简单的启动

class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

和默认值控制器

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

不管我怎么称呼我在控制台看到同样的 404 错误:

No matter what I call I see in console same 404 error:

Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/values
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 105.0181ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/api/values
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 2.6016ms 404
etc

我尝试使用 app.UseMvcWithDefaultRoute(); 添加默认路由并手动指定它.我尝试在使用默认路由时删除路由属性.还尝试添加 AddControllersAsServices().但结果仍然相同 - 404.当我在 app.Run 中设置自定义处理程序时,它可以正常工作.

I tried adding default route both with app.UseMvcWithDefaultRoute(); and specifying it manually. I tried removing route attributes when used the default route. Also tried adding AddControllersAsServices(). But result is still same - 404. When I setup custom handler in app.Run then it works without any issues.

csproj(我已经替换了默认的 Microsoft.AspNetCore.All 依赖项,但路由仍然不起作用)

csproj (I have replaced default Microsoft.AspNetCore.All dependency, but routing still does not work)

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
  </ItemGroup>

</Project>

推荐答案

这很难追查,但问题归结为 .csproj 中的这个:

This was rather difficult to track down, but the problem boils down to this in your .csproj:

<Project Sdk="Microsoft.NET.Sdk">

在构建 Web 应用程序时,您需要改为引用 Web Sdk,如下所示:

As you are building a web application, you need to instead reference the Web Sdk, as follows:

<Project Sdk="Microsoft.NET.Sdk.Web">

我设法通过这个小改动重现并解决了您的问题.

I managed to reproduce and fix your issue with this small change.

这篇关于路由不适用于自托管 Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:09