![](/img/trans.png)
[英]C# WebAPI - Calling POST Method Within Controller with Model as Parameter
[英]Post WebApi Method With Parameter C#
我只想在asp.net mvc中發布Webapi方法,后操作方法看起來像
[HttpPost]
[Route("api/agency/Dashboard")]
public HttpResponseMessage Index(getCookiesModel cookies)
{
//code here
}
我正在發送這樣的帖子請求
string result = webClient.DownloadString("http://localhost:11668/api/agency/dashboard?cookies=" + cookies);
和getCookiesModel
public class getCookiesModel
{
public string userToken { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public long userId { get; set; }
public string username { get; set; }
public string country { get; set; }
public string usercode { get; set; }
}
但是找不到此返回404頁面。 請幫我解決這個問題。
DownloadString
是GET請求,並且由於該操作需要POST,因此您可以看到可能存在問題的地方。
考慮使用HttpClient
發布請求。 如果在正文中發送有效負載,則不需要查詢字符串,因此您還需要更新客戶端調用URL。
var client = new HttpCient {
BaseUri = new Uri("http://localhost:11668/")
};
var model = new getCookiesModel() {
//...populate properties.
};
var url = "api/agency/dashboard";
//send POST request
var response = await client.PostAsJsonAsync(url, model);
//read the content of the response as a string
var responseString = await response.Content.ReadAsStringAsync();
Web API應遵循以下語法
[HttpPost]
[Route("api/agency/Dashboard")]
public IHttpActionResult Index([FromBody]getCookiesModel cookies) {
//code here...
return Ok();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.