This question already has an answer here:
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>
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.
For me it helped adding AddApplicationPart
after AddMvc
like this:
.AddMvc()
.AddApplicationPart(typeof(Startup).Assembly)
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