7

I have a controller named LoginController with a Get method with a signature of:

public string Get(string Key, string Code, string UserID, string Password)

I want to be be able to invoke it with a call similar to:

http://localhost:1234/api/Login/KeyValue/CodeValue/UserValue/PasswordValue

I cannot get this to work. If I invoke the call with:

http://localhost:1234/api/Login?Key=KeyValue&Code=CodeValueUserID=UserValue&Password=PasswordValue 

The call is successful.

I've tried adding routes such as below to Global.asax

 routes.MapHttpRoute(name: "Login", routeTemplate: "api/{controller}/{action}/{Key}/{Code}/{UserID}/{Password}",
                defaults: new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional });

or

 routes.MapHttpRoute(name: "Login", routeTemplate: "api/{controller}/{Key}/{Code}/{UserID}/{Password}",
                defaults: new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional });

These do not seem to work. Where am I going wrong or is this even possible? I was able to do this in the RC version of WebApi with MVC3.

2 Answers 2

11

It seems you are missing the action in your request (/api/Login/KeyValue/CodeValue/UserValue/PasswordValue). Try /api/Login/Get/KeyValue/CodeValue/UserValue/PasswordValue instead, if you intend to use the first route.

If you want to be able to call it without the action specified and default to "Get", you have to specify a default action:

defaults: new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional, Action = "Get" }

I have successfully tried this in an ASP.NET MVC 4 project (Visual Studio 2012 RC):

Creating a LoginController with action:

public string Get(string Key, string Code, string UserID, string Password)
{
    return Key + Code + UserID + Password;
}

And mapping the route in Global.asax.cs:

 RouteTable.Routes.MapHttpRoute(null, "api/{controller}/{Key}/{Code}/{UserID}/{Password}",
            new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional, Action = "Get"});

If it is not working for you, maybe another route is catching the request or the route is not being registered.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm in agreement, I need to expose an API to a third party so the format is out of my control. I've tried what you suggested but neither approach works (adding GET or specifying default action). I also tried adding [HttpGet] to the controller label.
You're correct. I mistakenly added a RegisterRoutes method to Global.asax (as in MVC3), once I added it in RouteConfig.cs all it works fine. Thanks.
0

You are trying to make routing without action. It works.

But in this case, you should use attribute in your controller to label action methods. You can use the following attributes: HttpGet, HttpPut, HttpPost, HttpDelete, AcceptVerbs, NonAction.

For more information read about it in article:

http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

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.