属性路由未正确映射
Attribute Route is not Mapping Correctly
我的 Startup.cs 设置如下:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BotApiV2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddApiExplorer();
services.AddRazorPages();
services.AddSwaggerGen();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
我的控制器是这样设置的:
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using BotApiV2.DataLayer;
namespace BotApiV2
{
[ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ValuesController))]
[Route("api/values")]
public class ValuesController : ControllerBase
{
[HttpGet]
[Route("test")]
public ActionResult Test()
{
var x = new BotDatabaseContext();
var t = new Dictionary<string, string>
{
{ "test1", "value1" },
{ "test2", "value2" },
{ "test3", "value3" }
};
return new JsonResult(t);
}
}
}
浏览/api/values/test
却找不到。是不是设置错了什么?我的目标是 .NET 5.0(我也尝试过 .NET Core 3.1,结果相同)。
您在启动时映射了 razor 页面路由。如果你想让你的控制器路由工作,你需要映射你的控制器路由。
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
我的 Startup.cs 设置如下:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BotApiV2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddApiExplorer();
services.AddRazorPages();
services.AddSwaggerGen();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
我的控制器是这样设置的:
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using BotApiV2.DataLayer;
namespace BotApiV2
{
[ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ValuesController))]
[Route("api/values")]
public class ValuesController : ControllerBase
{
[HttpGet]
[Route("test")]
public ActionResult Test()
{
var x = new BotDatabaseContext();
var t = new Dictionary<string, string>
{
{ "test1", "value1" },
{ "test2", "value2" },
{ "test3", "value3" }
};
return new JsonResult(t);
}
}
}
浏览/api/values/test
却找不到。是不是设置错了什么?我的目标是 .NET 5.0(我也尝试过 .NET Core 3.1,结果相同)。
您在启动时映射了 razor 页面路由。如果你想让你的控制器路由工作,你需要映射你的控制器路由。
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});