简体   繁体   English

Web API controller 自定义名称触发错误

[英]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.

相关问题 对于不匹配的控制器操作,Asp.net 核心 Web api 自定义错误响应? - Asp.net core web api custom error response for unmatched controller action? ng2-file-upload不会触发控制器方法.NET Core Web API - ng2-file-upload doesn't trigger the controller method .net core web api 将对象绑定到 Web API 端点时指定自定义属性名称 - Specifying custom property name when binding object to Web API endpoint 在 asp.net core 2.2 web api 中的 AuthenticationHandler 中获取控制器名称 - Get controller name in AuthenticationHandler in asp.net core 2.2 web api 如何在 C# / ASP.NET Core 6 Web API 服务器 OData controller 中执行自定义 function 路由? - How to do custom function routing in C# / ASP.NET Core 6 Web API Server OData controller? Controller calls web api, how to handle http error codes returned from API - Controller calls web api, how to handle http error codes returned from API ASP Web API:在将 object 序列化为 Z0ECD11C1D7A287401DZAA14FZ 时指定自定义字段名称 - ASP Web API: Specifying custom field name when serializing object to JSON Web API controller GET 方法执行成功 - 但是在客户端浏览器上给出内部服务器错误(500) - Web API controller GET method executing successfully - But giving Internal server error(500) at client browser ASP.NET Core Web Api 控制器端点都返回 404 错误 - ASP.NET Core Web Api controller end points are all returning 404 error getting 405 http error when reading form data in asp.net core web api controller - getting 405 http error when reading form data in asp.net core web api controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM