简体   繁体   English

ASP.Net 中具有相同路由名称的 Post Action 方法重载 Web API Controller

[英]Post Action method overload with same route name in ASP.Net Web API Controller

Is it possible to have two actions with same route name and same method but different parameter?是否可以有两个具有相同路由名称和相同方法但参数不同的操作? I have tried this:我试过这个:

[HttpPost]
[Route("gstr4")]
public HttpResponseMessage SubmitGSTR4([FromBody] RequestPayloadWithoutSign requestPayload)
{ }

[HttpPost]
[Route("gstr4")]
public HttpResponseMessage FileGSTR4([FromBody] RequestPayloadWithSign requestPayload)
{ }

I received a Status Code of 500 (InternalServerError) and here is raw response:我收到 500 (InternalServerError) 的状态代码,这是原始响应:

{"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nFileGSTR4 on type APIPortal.Controllers.GSTR4Controller\r\nSubmitGSTR4 on type APIPortal.Controllers.GSTR4Controller","ExceptionType":"System.InvalidOperationException","StackTrace":"   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}

It's not possible for the same method type as http Get for multiple methods.无法获取与 http 相同的方法类型获取多种方法。 You should have a different route你应该有不同的路线

It is only possible with different HTTP verbs.只能使用不同的 HTTP 动词。 Otherwise, you have to define different routes.否则,您必须定义不同的路线。

https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

It's not possible to have many actions with the same route and the same HTTP verb.不可能有许多具有相同路线和相同 HTTP 动词的动作。

What you could do is to have a single action which accepts a base class and a custom resolver.您可以做的是有一个接受基本 class 和自定义解析器的操作。

For example:例如:

public abstract class RequestPayload 
{
}

public class RequestPayloadWithoutSign : RequestPayload
{
}

public class RequestPayloadWithSign : RequestPayload
{
   public string Sign { get; set; }
}

And a custom JSON resolver attached to your JSON deserializer.以及连接到 JSON 解串器的定制 JSON 解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向ASP.NET Web API Controller添加显式操作路由 - Adding an explicit action route to ASP.NET Web API Controller 混合web api动作和非api动作相同的控制器asp.net核心 - Mix web api action and non api action same controller asp.net core ASP.NET MVC从同一控制器发布到控制器操作 - ASP.NET MVC post to controller action from same controller 在ASP.NET Web API 2中自动将操作名称设置为路由属性 - Set action name as route attribute automatically in ASP.NET Web API 2 没有路由名称,带有属性路由的ASP.NET Web API控制器不起作用 - ASP.NET Web API controller with attribute routing doesn't work without a route name ASP.NET Web API多个帖子消息单个控制器或动作 - ASP.NET Web API multiple post messages single controller or action 具有相同路由前缀 ASP.NET Web Api 的多个控制器类型 - Multiple Controller Types with same Route prefix ASP.NET Web Api 如何在ASP.NET MVC中将/ controller / action-name之类的URL路由到controller / action_name - How to route URLs like /controller/action-name to controller/action_name in ASP.NET MVC 找不到ASP.NET Web API路由控制器 - ASP.NET Web API Route Controller Not Found 在它绑定到 asp.net web api 2 中的控制器操作方法之前,无论如何要修改请求正文 - Is there anyway to modify request body before it gets bind to controller action method in asp.net web api 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM