Web API操作成功执行但我在客户端应用程序中看不到任何响应

时间:2014-07-01 15:41:09

标签: 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")]
public IUser CreateNew(NewUserAccountInfo newUserAccountInfo)
{
    return _provider.CreateNew(newUserAccountInfo.FullName, newUserAccountInfo.Email, newUserAccountInfo.PasswordHash);
}

MVC app控制器

IUser user = new WebAPIClient()
             .PostAsJsonAsync<NewUserAccountInfo, IUser>
              ("api/membership/CreateNewUserFromAccountInfo", 
              newUserAccountInfo
              ).Result;

来自MVC应用引用的类库中的WebAPIClient类:

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);
    }
}

通过POSTMAN(REST客户端)请求后返回的结果

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
}

0 个答案:

没有答案