无法直接路由到api端点。
应该在 localhost:5511/api/Test/getcollection?testCode=A
上找到我的api控制器“ TestController”,但它不会路由并重新调整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页面.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");
}
}
}