如何知道ASP.Net MVC中的确切异常类型

时间:2016-12-26 22:03:32

标签: c# asp.net-mvc asp.net-mvc-4 exception asp.net-mvc-5

我有一个ASP.Net Web应用程序,我正在执行登录操作。但是,当我调用登录服务时,我得到了以下的执行。

System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]


if (loginType == LoginTypeEnum.Google)
{
    result = AuthenticateByGoogle(username, password);
}
catch (Exception ex)
{
    string x = ex.GetType().ToString(); // What to write here to get the exact exception
    return x;
}

出于调试目的,我将返回字符串x并将其显示在前端,方法是将其添加到错误包中。我认为FaultException是一种通用的异常类型。那么我怎样才能确定“ex”中的确切异常。我是初学者所以请指导我。

1 个答案:

答案 0 :(得分:2)

ex.GetType()的名称。;将返回确切的异常类型。使用方式如下:

catch (Exception ex)
{
  string x = ex.GetType().Name;
  return x;
}