【发布时间】:2021-05-25 10:21:30
【问题描述】:
错误信息:
操作“SampleCusAppAPI.Controllers.CustomerController.GetCustomer (SampleCusAppAPI)”没有属性路由。使用 ApiControllerAttribute 注解的控制器上的操作方法必须是属性路由。
代码:StartUp.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers(); // Error Occured in this line.
});
}
Web API 代码:CustomerController
namespace SampleCusAppAPI.Controllers
{
[ApiController]
//[System.Web.Http.Route("api/[controller]")]
public class CustomerController : ControllerBase
{
SampleTestingDBContext objSampleTestingDBContext = new SampleTestingDBContext();
[System.Web.Http.Route("api/Customer/GetCustomer")]
[System.Web.Http.HttpGet]
public IQueryable<Customer> GetCustomer()
{
return objSampleTestingDBContext.Customers.OrderByDescending(x => x.CustomerId);
}
}
}
【问题讨论】:
-
这个错误清楚地说明了这个
GetCustomer(SampleCusAppAPI),那个方法在哪里?它应该用路由属性装饰 -
@king king,你能发例子/链接吗?
-
示例就在您发布的内容中,但代码是针对
GetCustomer(),而不是针对GetCustomer(SampleCusAppAPI),所以我才问这个方法在哪里。 -
@king king,我的 api 控制器 getCustomer() 只有一种方法。但是发生了这样的错误(GetCustomer(SampleCusAppAPI))。此错误发生在 app.UseEndpoints 中。
-
@king king 请给我发送任何 web api 路由示例链接。我是核心新手。
标签: asp.net entity-framework asp.net-core asp.net-web-api asp.net-core-webapi