微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Web API Post方法无效

我有一个webapi控制器,以下是一个post方法.
public HttpResponseMessage Register(string email,string password)
   {

   }

我如何从浏览器进行测试?

当我用浏览器从浏览器测试时,它没有击中控制器.

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

它给了我以下错误.

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

你能帮我么???

解决方法

您收到错误,因为您无法以这种方式将多个参数传递给WebApi.

第一种选择:
您可以通过以下方式创建一个类并从body传递数据:

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

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

第二种选择:

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

并以这种方式传递json数据:

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

更新:

如果您想从URL获取数据,那么您可以使用属性路由.

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

注意:网址应为:http:// localhost:50435 / api / SignUp / sini @ gmail.com / sini @ 1234不要忘记在WebApiConfig中启用属性路由如果使用这种方式,则会出现安全问题.

原文地址:https://www.jb51.cc/html/228187.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐