ASP.Net Core Web API custom route not working

白昼怎懂夜的黑 提交于 2021-02-08 19:36:31

问题


I have a ASP.Net Core Web API project, and the following controller:

[Route("/api/v0.1/controller")]
[ApiController]
public class MyController : ControllerBase
{
   [HttpGet("/test")]
   public ActionResult Test() { return null; }
}

but when I run the project, at /api/v0.1/controller/test I get "page not found" and I don't see where I made a mistake.


回答1:


Your method route template contains prefixed /, due to that, the app route couldn't find the appropriate path.

Modify test method route template as follows.

[HttpGet("test")]
public ActionResult Test() { return null; }  

More at Routing to controller actions in ASP.NET Core



来源:https://stackoverflow.com/questions/58798420/asp-net-core-web-api-custom-route-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!