类型' System.NullReferenceException'的未处理异常发生在WebApi返回的mscorlib.dll中

时间:2016-07-25 16:59:45

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-web-api

我有使用WebApi的MVC项目。我试图在我的数据库中调用更新存储过程。更新通过我的webapiresponse工作,我的项目暂停,我收到错误"未处理的类型' System.NullReferenceException'发生在mscorlib.dll"。对于我的生活,我无法弄清楚它的含义是无效的。

这是我的MVC控制器中的方法:

        public void UpdateNCMSID(string ncmsId)
    {
        contractorId = UserInfo.targetCompanyID;
        CommonClient.UpdateNCMSID(contractorId, ncmsId);
    }

以下是WebApi客户端方法:

public static HttpContent ConstructNCMSContent(int CompanyID, string vchNCMSIDNumber)
    {
        return new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("CompanyID", CompanyID.ToString()),
            new KeyValuePair<string, string>("vchNCMSIDNumber", vchNCMSIDNumber.ToString())

        });
    }
    public Task<int> UpdateNCMSID(int CompanyID, string vchNCMSIDNumber)
    {
        try
        {
            var content = ConstructNCMSContent(CompanyID, vchNCMSIDNumber);
            var uri = new Uri("api/UpdateNCMSID/", UriKind.Relative);
            var response = PerformPost<int>(uri, content, true);
            return response;


        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

以下是WebApi服务中的方法:

        [HttpPost]
    [Route("api/UpdateNCMSID")]
    public IHttpActionResult UpdateNCMSID(ParametersNCMSModel pm)
    {

        var result = _db.sxUpdateNCMSIDNumberByCompanyID(pm.CompanyID, pm.vchNCMSIDNumber);
        return Ok(result);
    }

两个参数都有值,下面的代码中出现错误:

        protected async Task<T> PerformPost<T>(Uri uri, HttpContent content, bool IsAuthenticated)
    {
        _webApiClient = new HttpClient { BaseAddress = _baseAddress }; 
       if (IsAuthenticated) AppendDefaultHeaders(_webApiClient);
       var webApiResponse = await _webApiClient.PostAsync(uri,content);
       var responseContent = await webApiResponse.Content.ReadAsStringAsync();
       return JsonConvert.DeserializeObject<T>(responseContent);
    }

错误发生在这一行:

var webApiResponse = await _webApiClient.PostAsync(uri,content);

程序停止并重新启动后,我可以看到更新确实发生在数据库中。有人请告诉我这里缺少什么吗?

非常感谢任何帮助!

0 个答案:

没有答案
相关问题