简体   繁体   中英

Web api routing two controller similar action and parameter not populating parameter value in one action

I have two webapi controller ControllerA and ControllerB. Each has got one POST action ie

ControllerA

[HttpPost]
public HttpResponseMessage Action1(CustomObjectA req, string pan="")

ControllerB

 [HttpPost]
 public HttpResponseMessage Action2(CustomObjectB req, string mpxn="")

My web api routing is as shown below

  config.Routes.MapHttpRoute(
      name: "Purchase",
       routeTemplate: "{controller}/{pan}",
       defaults: new
           {
               controller  = "ControllerA"
               pan = RouteParameter.Optional
            });

  config.Routes.MapHttpRoute(
      name: "Vend",
      routeTemplate: "{controller}/{mpxn}",
      defaults: new
      {
           controller = "ControllerB",
           mpxn = RouteParameter.Optional
      });

When i invoke POST request from fiddler to /ControllerA/1 it works fine ie value for pan parameter in Action1 is set to 1 but when i invoke POST request from fiddler to /ControllerB/2, it comes to Action2 but the value for mpxn is not set and it is "". Can anyone please correc me the mistake i have done in routing?

Thanks

hope these links help to you: attribute routing-1 attribute routing-2

Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources.

The earlier style of routing, called convention-based routing, is still fully supported. In fact, you can combine both techniques in the same project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM