C# 为什么Asp.net核心Web API 2.0返回Http错误500
我已经将api移动到一个不同的文件夹结构中,而不是模板通常提供的文件夹结构 结构看起来像这样C# 为什么Asp.net核心Web API 2.0返回Http错误500,c#,asp.net-core-2.0,asp.net-core-webapi,C#,Asp.net Core 2.0,Asp.net Core Webapi,我已经将api移动到一个不同的文件夹结构中,而不是模板通常提供的文件夹结构 结构看起来像这样 API Controllers LoginController.cs LoginController有一个基本方法 [Route("api/[Login]")] public class LoginController : ControllerBase { [HttpGet] public ActionResult<IEnumerab
API
Controllers
LoginController.cs
LoginController有一个基本方法
[Route("api/[Login]")]
public class LoginController : ControllerBase
{
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
}
解决方案构建良好。当我尝试使用以下url访问页面时,它只是设置
localhost is currently unable to handle this request.
HTTP ERROR 500
是否需要添加一些设置以返回内容。您没有定义默认路由,这很好,但是您完全依赖于定义了属性路由的每个控制器和操作。在您的
LoginController
上,您确实有一个路由属性,但它不正确。括号用于替换某些路由值,如区域、控制器等。;这并不是说你的实际控制员的名字应该在里面。换句话说,您需要[Route(“api/Login”)]
或[Route(“api/[controller]””)
,后者将由ASP.NET Core用控制器名称Login
替换
此外,使用管线属性时,动作名称不再起作用。如果不定义路由,则与定义空路由相同,即[HttpGet(“”)
。因此,即使修复了控制器路由,该操作的URL仍然只是/api/login
,而不是/api/login/get
。如果希望get
,则需要将路由设置为:[HttpGet(“get”)]
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddOptions();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
app.UseMvc();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
localhost is currently unable to handle this request.
HTTP ERROR 500
https://localhost:44352/api/login/get
https://localhost:44352/API/Controllers/login/get