Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

asp.net web api - Web API Post Method not working

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 protected]&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???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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":"[email protected]",
  "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/[email protected]/sini@1234
Don't forget to enable attribute routing in WebApiConfig
If you use this way, you will have security issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...