以下代码构建但不起作用。当我运行代码时,它会构建并托管API @ http://localhost:5000但是当我尝试使用http://localhost:5000/api/test访问同一个表单时,它不起作用?我错过了什么?
Program.cs的
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleAppCoreV2
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
}
public class TestController
{
[HttpGet("api/test")]
public object Test()
{
return new
{
message = "First test",
time = DateTime.Now
};
}
}
}
}
ConsoleAppCoreV2.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.0-preview1-final" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.0-preview1-final" />
</ItemGroup>
</Project>
在自包含的.NET Core中是否有在控制台应用程序上托管的WebApi的任何工作示例(无论是核心1.1还是2.0预览版)?
答案 0 :(得分:-1)
您应该将TestController类放在Program类之外。