我建立了一個REST API與具有4個參數POST方法控制器像這個 -REST的WebAPI POST方法與參數沒有被調用
[HttpPost]
public void SaveSession([FromBody] string userId, [FromBody] DateTime issueDateTime, [FromBody] string browserType, [FromBody] string salt)
{
// Params need to be changed
_sessionService.SaveSession(userId, issueDateTime, browserType, salt);
}
我應該如何張貼在客戶端的數據,我的意思是應該發送的數據格式是什麼?
我想這個格式 -
"userId=abc&DateTime=someDatetime&browserType=somebrowser&salt=somesalt"
它不工作,如果我試試這個,Web服務方法甚至沒有被稱爲
誰能告訴我正確的格式?
編輯: 這裏是我正在調用API-
const string endPoint = @"http://localhost:85/session/Test";
var postData = "userId=abc&DateTime=someDatetime&browserType=somebrowser&salt=somesalt"
var request = (HttpWebRequest) WebRequest.Create(EndPoint + parameters);
request.Method = "POST";
request.ContentLength = 0;
request.ContentType = "application/x-www-form-urlencoded";
if (!string.IsNullOrEmpty(postData) && Method == HttpVerb.POST)
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(postData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse) request.GetResponse())
{
var xmlDoc = new XmlDocument();
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
var responseStream = response.GetResponseStream();
if (responseStream != null)
{
xmlDoc.Load(responseStream);
}
return (xmlDoc);
}
謝謝!
告訴我們您的調用API,你的問題很可能在這個過程中 – Kristian
由於克里斯蒂安的那部分,我已編輯的職位。你能檢查一下,讓我知道什麼是錯的 – ManJan