C# Web API操作成功执行,但在客户端应用程序中未看到响应
在ASP.NET MVC应用程序中,我对Web API上的操作发出POST请求。API中的操作成功执行。当我在REST客户端(如Postman for Chrome)中发出相同的请求时,我会看到一个有效的Json响应 但是,如果我从我的MVC应用程序调用,我看不到任何响应,并且在向API发出POST请求后,控制流消失。MVC应用程序一直在等待,好像 这是我的密码 Web APIC# Web API操作成功执行,但在客户端应用程序中未看到响应,c#,asp.net-mvc,asp.net-web-api,asp.net-web-api2,C#,Asp.net Mvc,Asp.net Web Api,Asp.net Web Api2,在ASP.NET MVC应用程序中,我对Web API上的操作发出POST请求。API中的操作成功执行。当我在REST客户端(如Postman for Chrome)中发出相同的请求时,我会看到一个有效的Json响应 但是,如果我从我的MVC应用程序调用,我看不到任何响应,并且在向API发出POST请求后,控制流消失。MVC应用程序一直在等待,好像 这是我的密码 Web API [HttpPost] [ActionName("CreateNewUserFromAccountInfo")] pub
[HttpPost]
[ActionName("CreateNewUserFromAccountInfo")]
public IUser CreateNew(NewUserAccountInfo newUserAccountInfo)
{
return _provider.CreateNew(newUserAccountInfo.FullName, newUserAccountInfo.Email, newUserAccountInfo.PasswordHash);
}
MVC应用程序控制器
IUser user = new WebAPIClient()
.PostAsJsonAsync<NewUserAccountInfo, IUser>
("api/membership/CreateNewUserFromAccountInfo",
newUserAccountInfo
).Result;
通过邮递员(REST客户端)发出请求时返回的结果
您是否尝试过使用Fiddler检查您的HTTP请求?@tia:嗯?我刚发了一封邮递员的回信。在Fiddler中再次这样做有什么意义?@WaterCoolerv2:这将帮助您了解浏览器是否发送了请求,以及浏览器是否应该得到与您通过邮递员得到的相同的响应。@StriplingWarrior:这是一个POST请求。我不知道你会如何通过浏览器发送?@WaterCoolerv2:啊,这就澄清了一点。仅供参考,从浏览器发送HTTP POST并不困难。无论如何,如果您使用Fiddler,您应该能够比较MVC应用程序发送和接收的内容与邮递员发送和接收的内容。
public async Task<R> PostAsJsonAsync<T, R>(string uri, T value)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_baseUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
await PrintRequestBodyToDebugWindowAsync(value, new JsonMediaTypeFormatter());
// The flow of control just disappears after this line
// and never gets to the next line
var response = await client.PostAsJsonAsync(uri, value);
// The flow of control never gets here
if (response.IsSuccessStatusCode) return await response.Content.ReadAsAsync<R>();
else return default(R);
}
}
Request Uri: http://localhost:54488/api/membership/CreateNewUserFromAccountInfo
Method: POST
Request Body: {"FullName":"Aamir Khan","Email":"aamir@khan.com","PasswordHash":"hello"}
Response:
{
"Id": 15,
"IsExternal": false,
"ExternalId": null,
"ExternalProviderName": null,
"UserType": "Healthcare Seeker",
"Verified": false,
"VerificationCode": "375b8d2f-67df-433d-9e95-c84c30462b80",
"VerificationCodeExpiresOn": "9999-12-31T23:59:59.9999999",
"PasswordResetCode": null,
"DefaultPasswordChanged": false,
"HasFilledBasicProfile": false,
"CreationDateTime": "2014-07-01T20:47:45.1405416+05:30",
"FullName": {
"Id": 2,
"Name": "FullName",
"Value": "Aamir Khan",
"Description": null,
"PrivacyLevel": {
"Id": 1,
"Name": "Private",
"Description": "Visible only to me"
}
},
"Email": {
"Id": 1,
"Name": "Email",
"Value": "aamir@khan.com",
"Description": null,
"PrivacyLevel": {
"Id": 1,
"Name": "Private",
"Description": "Visible only to me"
}
},
"Profiles": [
null,
null,
null
],
"BasicProfile": null,
"MedicalProfile": null,
"EmergencyProfile": null
}