Get() 不工作的简单 ping 控制器的属性路由
Attribute routing for simple ping controller with Get() not working
我有简单的 asp.net 核心网络 api 和一个微调控制器,returns 对 Get 请求为真。 PingController.cs 如下所示:
[Route("api/[Controller]")]
public class PingController : Controller
{
[HttpGet]
public IActionResult Get()
{
return Ok(true);
}
}
为什么导航到控制器 (http://localhost:56103/api/Ping)
返回 404?
我在控制器顶部添加了路由,并为特定操作添加了 HttpMethod。我在这里遗漏或不理解的是什么?
当我在 Startup.cs 中添加 app.UseMvcWithDefaultRoute()
时,控制器工作正常。 (这也让我很困惑。)
Startup.cs 如下所示:
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup()
{
Configuration = BuildConfiguration();
}
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
ConfigureRoutes(app);
}
private static void ConfigureMvc(IServiceCollection services, Config config)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)// auto generated
.AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; })
.AddControllersAsServices();
}
private static void ConfigureRoutes(IApplicationBuilder app)
{
//app.UseMvcWithDefaultRoute();
}
}
您需要在启动时添加UseMvc()
或UseMvcWithDefaultRoute()
来定义路由。
UseMvcWithDefaultRoute
将名为 'default' 的默认路由添加到请求执行管道并等于
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我有简单的 asp.net 核心网络 api 和一个微调控制器,returns 对 Get 请求为真。 PingController.cs 如下所示:
[Route("api/[Controller]")]
public class PingController : Controller
{
[HttpGet]
public IActionResult Get()
{
return Ok(true);
}
}
为什么导航到控制器 (http://localhost:56103/api/Ping)
返回 404?
我在控制器顶部添加了路由,并为特定操作添加了 HttpMethod。我在这里遗漏或不理解的是什么?
当我在 Startup.cs 中添加 app.UseMvcWithDefaultRoute()
时,控制器工作正常。 (这也让我很困惑。)
Startup.cs 如下所示:
public class Startup
{
public IConfiguration Configuration { get; set; }
public Startup()
{
Configuration = BuildConfiguration();
}
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
ConfigureRoutes(app);
}
private static void ConfigureMvc(IServiceCollection services, Config config)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)// auto generated
.AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; })
.AddControllersAsServices();
}
private static void ConfigureRoutes(IApplicationBuilder app)
{
//app.UseMvcWithDefaultRoute();
}
}
您需要在启动时添加UseMvc()
或UseMvcWithDefaultRoute()
来定义路由。
UseMvcWithDefaultRoute
将名为 'default' 的默认路由添加到请求执行管道并等于
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});