1
我有在獲得自定義異常消息從我的Web API解決方案返回相當多的困難,覆蓋異常消息。這是比這更復雜一些:如何處理ASP.NET Web API
我想用我自己的覆蓋異常的只讀屬性:
public class CustomException : Exception
{
public CustomException(string message)
: base(message)
{
}
public CustomException(string message, Exception inner)
: base(message, inner)
{
}
}
不過,我也有一個全球性的異常處理程序:
public class GlobalExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
if (context.Exception.Message == "Password has already been used in the past...1")
{
throw new CustomException("some msg", context.Exception);
}
NLogger.LogError("Global Error Handler", context.Exception);
}
}
當我拋出的錯誤,我這樣做,如:
if (some condition)) throw new CustomException("some msg");
然後我抓住它像的方法:
catch (CustomException ex)
{
throw ex;
}
catch (Exception ex)
{
NLogger.LogError(ex);
throw;
}
我如何設置消息是「一些味精」?我想要做的是讓api返回1-2個用例相關的錯誤消息,並將customErrors模式設置爲on。
你可以添加一個解釋你的代碼? – Alex
這讓我需要和我在一起,謝謝! – RandomUs1r