0

我在我的 asp.net mvc Web 应用程序中将以下代码作为我的 Action 方法的一部分:-

catch (Exception ex)
{
      using (var client = new WebClient())
      {

          var query = HttpUtility.ParseQueryString(string.Empty);
          XmlDocument do2c = new XmlDocument();
          query["username"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiUserName"];
          query["password"] = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiPassword"];
          query["assetType"] = RouteData.Values["controller"].ToString();
          query["operation"] = "DeleteAsset";
          query["assetName"] = vmj.Resource.RESOURCENAME;
          query["accountName"] = vmj.Customer.NAME;
          query["siteName"] = sitename;
          string apiurl = System.Web.Configuration.WebConfigurationManager.AppSettings["ApiURL"];
          var url = new UriBuilder(apiurl);
          url.Query = query.ToString();
          try
          {
              string xml = client.DownloadString(url.ToString());
              XmlDocument doc2 = new XmlDocument();
              doc.LoadXml(xml);


          }
          catch (WebException ex2)
          {
              ModelState.AddModelError(string.Empty, "Error occurred:" + ex2.InnerException.Message);
          }
          //}
          //}
      }
      ModelState.AddModelError(string.Empty, "Error occurred:" + ex.InnerException.Message);
  }
}

但是当Exception ex发生这种情况时,我无法显示它的 InnerException.Message,并且在尝试引用 InnerException.Message 时我会得到一个空引用异常。虽然我添加了一个刹车点,并且 ex 对象包含内部异常和描述异常的消息。所以知道发生了什么吗?

4

0 回答 0