0

Can not send parameter name "action" by url in asp.net web api 2.0.

Example:

http://host:port/controller_name?action=2&...

if you do so:

 config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{action}",
                defaults: new
                {
                    action=RouteParameter.Optional
                }
            );

method in controller:

   public HttpResponseMessage Get(int action)
    {
        return ResponseXml();
    }

gives an error message:

in the dictionary path name of the parameter "action" is contained in the URL-address more than once

How to pass parameter name "action" as a parameter, rather than the action method ?

thanks

2
  • If you want to your action param through querystring(?action=..), then you don't need to set in the route map. In your current routeTemplate, your url should be like /controllername/actionname/xyz, but if that is reqriured, why can't you change your routeTemplate to use other than action? Commented May 5, 2016 at 10:49
  • @Karthik M R I need to receive data strictly according to the protocol which format: host:port/controller_name?action=2&login=, first parameter should be parameter name "action" Commented May 5, 2016 at 11:18

2 Answers 2

2

Since the name action is included in the querystring part(?action=2), no need to change the route map. The framework will bind the value to the action paramter in the action method. Remove the extra {action} in routeTemplate. And since your url format doesn't contain {action} host:port/controller_name?action=2&login=, remove {action} from routemap. So, your route map will be

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new
            {
                id=RouteParameter.Optional
            }
        );
Sign up to request clarification or add additional context in comments.

3 Comments

if you do so, receive an error: No action was found on the controller 'Values' that matches the name '2' .Because is a binding parameter value to the method controller
remove api from routeTemplate. have updated my answer
its clear. Thanks. I thought that all the parameters should be strictly specified in the route.
0

You could try using Attribute Routing and include it in the route template.

//GET [controller_route]/2
[HttpGet]
[Route("{action:int}")]
public HttpResponseMessage Get(int action)
{
    return ResponseXml();
}

which will let you use the following url

http://host:port/controller_name/2

where action parameter will be mapped to 2.

Remember to enable the attribute routing during configuration.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.