Routing is not working with self-hosted web API [duplicate]

冷暖自知 提交于 2019-11-26 20:58:11

问题


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>();
}

Then goes the simplest 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();
    }
}

And default values controller

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

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

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 (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>

回答1:


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

<Project Sdk="Microsoft.NET.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.




回答2:


For me it helped adding AddApplicationPart after AddMvc like this:

.AddMvc()
.AddApplicationPart(typeof(Startup).Assembly)



回答3:


you can try change code to this:

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

The atribute Route can use for asigning an specific route to a function on api or view.

then to call you can use:

'localhost:5000/api/values'



来源:https://stackoverflow.com/questions/50632468/routing-is-not-working-with-self-hosted-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!