我正在尝试在C#中使用MVC。因此,表单 Controller 使用身份验证类实例。我正在处理身份验证类中的无效凭据。但是我无法在消息框中显示该消息,因为它不在表单 Controller 类实例中。请帮忙
最佳答案
您不能简单地在身份验证类中抛出异常,并让您的主表单处理该异常吗?
// main form
try
{
User _loggedOnUser = Authenticate.GetLoggedOnUser();
}
catch (AuthenticationException ex)
{
MessageBox.Show(this, ex.Message, "Unable to authenticate user");
}
// Authenticate class
// ... do something
if (something == true)
throw new AuthenticationException("User account has been disabled");
或者,如果异常不合适,则使用返回值:
// main form
User _loggedOnUser = Authenticate.GetLoggedOnUser();
if (_loggedOnUser == null)
MessageBox.Show(this, "Unable to authenticate the user");
关于c# - 如何创建一个Windows App(C#),在其中可以在消息框中显示各种对象中的所有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1412614/