About the author

Brian Keating is a developer addicted to Microsoft Technologies.

Month List

RecentComments

Comment RSS

ASP MVC Controller Exception handlers

clock April 11, 2011 23:43 by author Brian Keating |

 

When you crate a new MVC project a view called Error.aspx is created for you in the Views/Shared folder.

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>

 
<asp:Content ID="errorTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Error
</asp:Content>
 
<asp:Content ID="errorContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Sorry, an error occurred while processing your request.
    </h2>
</asp:Content>

To instruct controller actions to use this Error handler you need to use the HandlErrorAttribute action filter, this is the default exception handler present in MVC.
HandleError is used to specify an exception type to handle and a View (and Master View if necessary) to display if an action method throws an unhandled exception that matches or is derived from the specified exception type.

Some points to note:

  • If no exception type is specified, then the filter handles all exception.
  • If no view is specified then the default Error view is used.
  • As mentioned earlier, exceptions are caught by base types, so it’s important to specify an order catching the most specific exception types first (much like a standard try catch code block.
  • The handler won’t get called in debug builds as it checks the HttpContext.IsCustomErrorEnabled (yellow screen of death is preferred)
  • e.g.
// Dont do this
[HandleError(Order=1, ExceptionType=typeof(Exception)]
[HandleError(Order=2, ExceptionType=typeof(ArgumentNullException, View=ArgNull)]

 

You’ll need to reverse the order, because if you want the ArgumentNullException to be handled differently, the exception shouldn’t get swallowed by the typeof(Exception) handler.




VS2010 Script chooser

clock January 19, 2011 23:26 by author Brian Keating |

In a visual studio 2010 asp page, start typing

<script src=

You’ll get presented with the following screen.

image

 

Choose the Pick URL … option and you’ll be presented with the following screen

image

I think you’ll figure out the rest Ninja




Linq in asp.net

clock September 25, 2010 23:35 by author Brian Keating |

A quick sample of how to use linq in your webpages

 

<% Model.ToList().ForEach(item =>
{ %>
  <tr>
      <td>
          <%: Html.ActionLink("Edit", "Edit", new { id = item.AlbumId })%> |
          <%: Html.ActionLink("Delete", "Delete", new { id = item.AlbumId })%>
      </td>
      <td><%: Html.Truncate(item.Title, 25)%></td>
      <td><%: Html.Truncate(item.Artist.Name, 25)%></td>
      <td><%: item.Genre.Name%></td>
  </tr>
<% }); %>

 

I said quick ey!  :-)




MVC Stories

clock September 8, 2010 20:53 by author Brian Keating |

Hi All,

Been a while since I've writen some posts, been pretty hectic hours at work and weekends building a house so my chances to blog have been limited.

I intend over the coming few days to give a few tips and tricks on MCV2.

Here's on gottya..

Be carefull how you name your formal arguements in your controller functions.

You can see from the screenshot below that I created two args, "Name" and "name"

By default the first matching case insensitive value will be applied to both variables by MVC...

Usually this will be avoided by good naming conventions but be carefull nonetheless :-)

 

 




Output and Encoding in ASP.net 4.0

clock June 29, 2010 23:07 by author Brian Keating |

Pre .NET 4.O

Prior to  ASP.NET 4.0 (and especially with MVC) when a user outputted information to a webpage they used <%= Server.HtmlEncode(modelViewStore.Content) %>

The reason for the Encoding is primiarily to prevent XSS (cross site script injection) whereby someone may try to inject some client side script or HTML Markup to vandalize a site or to steal valuable information.

This approach has a few shortcommings; like,

* Users may forget the encoding
* bit verbose

 


.NET 4.0

A new nugged has arrived:

<%: modelViewStore.Content %>