[英]Web API controller with custom name trigger error
Hi I'm a beginner with Web API.嗨,我是 Web API 的初学者。 So I created a simple function test like this.
所以我像这样创建了一个简单的 function 测试。
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
[HttpGet]
[Route("api/[controller]/test")]
public string[] test()
{
return new string[] { "test1","test2" };
}
Here I created a custom method test with route specification.在这里,我创建了一个带有路由规范的自定义方法测试。 But when I try https://localhost:44389/api/values/test , it says
{"errors":{"id":["The value 'test' is not valid."]},"title":"One or more validation errors occurred.","status":400,"traceId":"8000001f-0002-e900-b63f-84710c7967bb"}
但是当我尝试https://localhost:44389/api/values/test时,它说
{"errors":{"id":["The value 'test' is not valid."]},"title":"One or more validation errors occurred.","status":400,"traceId":"8000001f-0002-e900-b63f-84710c7967bb"}
Plese tell me know how to solve this issue.请告诉我如何解决这个问题。
You need to use the slash prefix if you want to ignore the route on controller:如果要忽略 controller 上的路由,则需要使用斜杠前缀:
[Route("/api/[controller]/test")]
Otherwise your route will be something like /api/values/api/values/test
.否则,您的路线将类似于
/api/values/api/values/test
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.