Author: Dino Esposito
Pub Date: 2014
Size: 15 Mb
Go deep into the architecture and features of ASP.NET MVC 5, and learn how to build web applications that work well on both the desktop and mobile devices. Web development expert Dino Esposito takes you through the web framework’s Model-View-Controller (MVC) design model, and covers the tools you need to cleanly separate business logic from the user interface. If you’re an experienced web developer new to ASP.NET MVC, this practical guide will get you going.
Discover how to:
- Build web applications that are easy to test and maintain
- Dive into the functions of controllers—the heart of an MVC site
- Explore the structure and behavior of a view engine
- Process a variety of input data using a custom model binder
- Automate the writing of input forms, and streamline validation
- Design websites for mobile devices, localization, and error handling
- Provide security by implementing a membership system
- Use Responsive Web Design to make sites mobile-friendly
ASP.NET MVC software design
Global error handling using an HTTP module
In ASP.NET, there’s just one way to capture any fatal exceptions: writing a handler for the Http Application object Error event. However, you can do this in two ways. You can write code directly in the application’s global.asax fie, or you can plug a made-to-measure HTTP module to the web.confi fie. The HTTP module registers its own handler for the Error application event. The two solutions are functionally equivalent, but the one based on the HTTP module can be turned on and off and modifid without recompiling the application. It is, in a way, less obtrusive.
When you consider a global error handler, you have a couple of ideas in mind: alerting the administrator about an exception, and logging the exception. Especially for the second task, an HTTP module seems like an easier-to-manage solution than having code in global.asax.
A tool that is very popular among ASP.NET developers is Error Logging Modules And Handlers (ELMAH). ELMAH is essentially made of an HTTP module that, once confiured, intercepts the Error event at the application level and logs it according to the confiuration of a number of back-end repositories. ELMAH comes out of an open-source project (code.google.com/p/elmah) and has a number of extensions, mostly in the area of repositories. ELMAH offers some nice facilities such as a webpage on which you can view all recorded exceptions and look more closely into each of them. Architecturally speaking, any error-reporting system specifially designed for ASP.NET can’t be much different from ELMAH.
Intercepting model-binding exceptions
A centralized error handler is also good at catching exceptions that originate outside the controller, such as exceptions caused by incorrect parameters. If you declare a controller method with one, for instance, integer argument and the current binder can’t match any posted value to it, you get an exception. Technically, the exception is not raised by the model binder itself; it is raised by an internal component of the action invoker while preparing the method call. If this component doesn’t fid a value for a non-optional method parameter, it just throws an exception.
This exception is not fied from within the controller code, but it still falls under the control of the overall try/catch block in the action invoker. Why doesn’t a global (or local) HandleError do the trick of trapping the exception? It does, but only if you turn on the custom error flg in the web.confi fie. With the custom error flg turned off, your only chance to intercept a model-binding error is through the centralized error handler in global.asax. Figure 5-2 shows the page served gracefully to the user and the email message sent to the administrator.