Proper coded examples of MVC 3 error handling
I'm coming from the Winforms world where I trapped everything and handled errors with messages to the user with MessageBox.Show(ex.message) or just logged it. Now I'm in the web (MVC 3) world and I can't seem to find a definitive coded example of how to employ proper error handling. Let me show you what I'm using:
User clicks on a link which runs a controller action:
Function SelectAlertKPIs() As ActionResult
Try
Return PartialView()
Catch ex As Exception
TempData("ErrorMessage") = "There was a problem during page load: " & ex.Message & vbCrLf & vbCrLf & ex.StackTrace
Return RedirectToAction("HttpError", "Error")
End Try
End Function
This view loads a Telerik grid with AJAX binding, so now the binding runs this action:
<GridAction()> _
Function _SelectAlertKPIs() As ActionResult
Try
Return PartialView(New GridModel(AlertKPIRepository.All()))
Catch ex As Exception
TempData("ErrorMessage") = "There was a problem during page load: " & ex.Message & vbCrLf & vbCrLf & ex.StackTrace
Return RedirectToAction("HttpError", "Error")
End Try
End Function
Which calls this method to get data:
Public Shared Function All() As IList(Of AlertKPI)
Dim l As IList(Of AlertKPI) = Nothing
Try
Using dc As New AdminAlertKPIDataContext(TripleDESSecurity.Decrypt(SharedData.PortalCnx))
l = (From d In dc.AdminAlertKPIs Order By d.ID Select New AlertKPI With {
.ID = d.ID,
.KPI = d.KPI,
.KPIName = d.KPIName,
.KPIDescription = d.KPIDescription
}).ToList
End Using
Return l
Catch ex As Exception
Throw
Finally
If Not l Is Nothing Then l = Nothing
End Try
End Function
These are my ErrorController actions:
Public Function HttpError() As ActionResult
Dim s As String = String.Empty
s = TempData("ErrorMessage")
ViewData("ErrorMessage") = "An error occurred. " & s
Return PartialView("Error")
End Function
Public Function Http404() As ActionResult
Try
ViewData("ErrorMessage") = "The page you requested could not be found"
Retu开发者_Go百科rn View("Error")
Catch ex As Exception
Return View("Error")
End Try
End Function
This is what I have in my web.config:
<customErrors mode="On" defaultRedirect="/Error/HttpError">
<error statusCode="404" redirect="/Error/Http404" />
</customErrors>
Now if I add code into my controller action to purposely throw:
Throw New HttpUnhandledException
Then it falls to the catch block, and then routes to the HttpError action.. Twice! and then my Error.aspx view never opens, instead I get either no JSON result or a 500 internal server error... makes not a lick of sense to me. Basically, I don't know what I'm doing.
And I could use some really good guidance....
You need to look into [HandleErrorAttribute]
- there is no need to polute your action methods with try catch blocks. You might want to implement your own error handler attribute for additional logging - which is what I'm doing today. This is today's post on exception handling: click here
Your web config is for exceptions that occur outside of MVC application pipeline. IIS will be able to read the web config and then re-direct user to the correct page. Others, please correct me if I'm wrong as I only started looking at this today.
精彩评论