简体   繁体   中英

How to get a POST variable from asp.net web api?

In c# asp.net 4.5.2, how do I get a post variable. I have this so far

    public async Task<HttpResponseMessage> PostMessage(HttpRequestMessage request, string pCompanyName, string pMessage, string language)
    {
    }

But these are all URL parameters. I want to move string pMessage into a POST body. From the client side I can send this by the data in jquery ajax.

data : {
    message : "test"
}

but how do I get this value from the c# side?

Thanks

You can use [FromBody] attribute

public async Task<HttpResponseMessage> PostMessage(HttpRequestMessage request, string pCompanyName, string language, [FromBody] string pMessage)
{
}

But you actually need something like

 [HttpPost]
 public async Task<HttpResponseMessage> PostMessage([FromUri] string pCompanyName, [FromUri] string language, [FromBody] string pMessage)
 {
 }

Create a ViewModel as the following

class InputMessage
{
    public string pMessage { get; set; }
}

And then use this class as the parameter of your method:

 public async Task<HttpResponseMessage> PostMessage(HttpRequestMessage request, string pCompanyName, InputMessage message, string language)
    {
      // message.pMessage
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM