在 RazorPages 应用中为 api 个端点启用路由
Enable routing for api endpoints in RazorPages app
无法直接路由到 api 端点。
我的 api 控制器 "TestController" 应该可以在 localhost:5511/api/Test/getCollection?testCode=A
找到,但它不会路由并返回 404。
我有一个 .net core 3.0 RazorPages 应用程序,但需要包含一些 api 端点。我认为我的端点没有被路由。
在 Startup.cs 配置方法中我有这个:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
这是我在控制器上的路由属性:
[Route("api/[controller]/[action]")]
GetCollection 操作:
[HttpGet]
public IActionResult GetCollection(string testCode)
{
List<string> retval = ...get a string list
return Ok(retval);
}
[编辑]
------ 好吧,一个更简单的例子,不会路由到 api ----------
Razor pages .net core 3.0 应用程序,添加一个新文件夹 /api 和一个控制器
前往
时收到 404
https://localhost:44340/api/lookup/getsomething
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddMvc();
}
// 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");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
api控制器
namespace aspnetcore1.Api
{
[Route("api/[controller]")]
[ApiController]
public class LookupController : ControllerBase
{
[HttpGet]
[Route("GetSomething")]
public IActionResult GetSomething()
{
return Ok("this is a test");
}
}
}
你在控制器上的路由属性应该是:
[Route("api/Test")]
方法上的路由属性应该是:
[HttpGet]
[Route("GetCollection")]
public IActionResult GetCollection(string testCode)
{
List<string> retval = ...get a string list
return Ok(retval);
}
编辑
看起来 Configure(IApplicationBuilder app, IWebHostEnvironment env)
方法中缺少这个:
app.UseMvc();
无法直接路由到 api 端点。
我的 api 控制器 "TestController" 应该可以在 localhost:5511/api/Test/getCollection?testCode=A
找到,但它不会路由并返回 404。
我有一个 .net core 3.0 RazorPages 应用程序,但需要包含一些 api 端点。我认为我的端点没有被路由。 在 Startup.cs 配置方法中我有这个:
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
这是我在控制器上的路由属性:
[Route("api/[controller]/[action]")]
GetCollection 操作:
[HttpGet]
public IActionResult GetCollection(string testCode)
{
List<string> retval = ...get a string list
return Ok(retval);
}
[编辑]
------ 好吧,一个更简单的例子,不会路由到 api ----------
Razor pages .net core 3.0 应用程序,添加一个新文件夹 /api 和一个控制器
前往
时收到 404
https://localhost:44340/api/lookup/getsomething
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddMvc();
}
// 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");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
api控制器
namespace aspnetcore1.Api
{
[Route("api/[controller]")]
[ApiController]
public class LookupController : ControllerBase
{
[HttpGet]
[Route("GetSomething")]
public IActionResult GetSomething()
{
return Ok("this is a test");
}
}
}
你在控制器上的路由属性应该是:
[Route("api/Test")]
方法上的路由属性应该是:
[HttpGet]
[Route("GetCollection")]
public IActionResult GetCollection(string testCode)
{
List<string> retval = ...get a string list
return Ok(retval);
}
编辑
看起来 Configure(IApplicationBuilder app, IWebHostEnvironment env)
方法中缺少这个:
app.UseMvc();