0
votes

In my WebApiConfig.cs file I have the following route defined: (it's the only route defined here)

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

The POST I'm making is to:

http://localhost:17138/api/Account/Login

My controller is this:

namespace WebAPI.Api
{
  public class AccountController : ApiController
  {
    [HttpPost]
    public HttpResponseMessage Login(string username,string password)
    {...

The error I'm getting is:

{"Message":"NoHTTPresourcewasfoundthatmatchestherequestURI'http://localhost:17138/api/Account/Login'.","MessageDetail":"Noactionwasfoundonthecontroller'Account'thatmatchestherequest."}

and from Phil's program it looks like the route should work. Any thoughts?

enter image description here

1
The route that you have created should work with this URI localhost:17138/api/Account/1234 . It doesn't know what Login is .Either you specify it in your router.Another thing that i doubt is not specifying controller in your route. - Tabish Sarwar
If you look carefully in tester as well. Its taking Login as Action. I hope you get it . Looking at your Controller Method. You shuold be doing localhost:17138/api/Account?username =""&password="" but with action in place you can append ?username =""&password="" in your URI. - Tabish Sarwar
I want to POST parameters to the URL: ../api/Account/Login What should my route registered be? api/{controller}/Login? - Peter Kellner
You mean you want to post username and password to this particular action 'Login' and not necessarily in the Url right? because passing username and password in Url is NOT a good idea as its not secure even if you use https. If you want to send them in a body, then have a wrapper class like Credentials having properties like UserName and Password and send this Credentials object via Json or Xml format that Web API's default Json and Xml formatters understand. - Kiran Challa
Kiran, True, I do not want to pass them in the url (though they would be secure even with https, my reason is I don't want to deal with url encoding issues). I do want to simply post parameters (in the POST data just like any other post, the don't need to be wrapped. "username=myuser&password=muypassword". I think I must be mis understanding how web api works. What should my route be? I thought the controller is Account and the Action is login and then when I pass in two parameters username and password it would find the correct method based on those two post parameters. Am I totally lost? - Peter Kellner

1 Answers

0
votes

I changed my parameters for my Login method to

public HttpResponseMessage Login(FormDataCollection formDataCollection)

and it worked. It seems I'm missing something about how POST parameters are handled because in the formDataCollection, both username and password are there. I'm not sure what that is not the same as

public HttpResponseMessage Login(string username,string password)