3

I have a webapi controller and following is a post method.

   public HttpResponseMessage Register(string email, string password)
   {

   }

How do I test from the browser?

When I test it from the browser with the following , it is not hitting the controller.

http://localhost:50435/api/SignUp/?email=sini@gmail.com&password=sini@1234

It is giving me the below error.

Can't bind multiple parameters ('id' and 'password') to the request's content.

Can you please help me???

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • 1
    Does that even compile? You have one "[" in your parameter list... – Marcus Höglund Oct 20 '16 at 06:27
  • To test action in WebApi I recommend you use [TestClient](https://blogs.msdn.microsoft.com/yaohuang1/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page/) or request composer in [Fiddler](http://www.telerik.com/fiddler) – Roman Marusyk Oct 20 '16 at 06:51
  • Also see http://stackoverflow.com/questions/38418787/how-to-test-a-web-api-post-method-which-receives-a-class – Roman Marusyk Oct 20 '16 at 06:56

1 Answers1

1

You get an error, because you cannot pass multiple parameter to WebApi in this way.

First option:
You can create a class and pass data through from body in this way:

public class Foo
{
    public string email {get;set;}
    public string password {get;set;}
}

public HttpResponseMessage Register([FromBody] Foo foo) 
{
    //do something
    return Ok();
}

Second Option:

public HttpResponseMessage Register([FromBody]dynamic value)
{
    string email= value.email.ToString();
    string password = value.password.ToString();
}

And pass json data in this way:

{
  "email":"abc@test.com",
  "password":"123@123"
}

Update:

If you wan to get data from URL, then you can use Attribute Routing.

[Route("api/{controller}/{email}/{password}")]
public HttpResponseMessage Register(string email, string password) 
{
    //do something
    return Ok();
}

Note:
URL should be : http://localhost:50435/api/SignUp/sini@gmail.com/sini@1234
Don't forget to enable attribute routing in WebApiConfig
If you use this way, you will have security issue.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76