Tuesday, June 26, 2012

Why doesn't ASP.NET MVC include these HtmlHelper methods?

These HtmlHelper extension methods provide typical use methods for creating action links that include areas. It feels kludgy to have to put "area" into the router values object/dictionary. It seems to me that this (or at least something similar along with additional appropriate overloads) should exist in the core implementation.
/// <summary>
/// Returns an anchor element (a element) that contains 
/// the virtual path of the specified area action.
/// </summary>
/// <param name="helper">The HTML helper instance that this method extends.</param>
/// <param name="linkText">The inner text of the anchor element.</param>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="areaName">The name of the area.</param>
/// <returns></returns>
public static MvcHtmlString ActionLink(
 this HtmlHelper helper, 
 string linkText, 
 string actionName, 
 string controllerName, 
 string areaName)
{
 return helper.ActionLink(
  linkText, 
  actionName, 
  controllerName, 
  areaName, 
  null);
}

/// <summary>
/// Returns an anchor element (a element) that contains 
/// the virtual path of the specified area action.
/// </summary>
/// <param name="helper">The HTML helper instance that this method extends.</param>
/// <param name="linkText">The inner text of the anchor element.</param>
/// <param name="actionName">The name of the action.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="areaName">The name of the area.</param>
/// <param name="htmlAttributes">An object that contains the HTML 
///  attributes to set for the element.</param>
/// <returns></returns>
public static MvcHtmlString ActionLink(
 this HtmlHelper helper, 
 string linkText, 
 string actionName, 
 string controllerName, 
 string areaName, 
 object htmlAttributes)
{
 return helper.ActionLink(
  linkText, 
  actionName, 
  controllerName, 
  new { area = areaName }, 
  htmlAttributes);
}

Thursday, March 01, 2012

Get the last ELMAH error

I've quickly become quite fond of using ELMAH for handling errors in ASP.NET web apps.  There is no better (i.e. custom or otherwise) way that I've found.

I updated an application that was using a messy custom error logging mechanism to use ELMAH and it's already saved me time in tracking down problems while updating the app.  The app has a custom errors page that shows a few error details to help the user communicate their problem.  It was displaying the old mechanism's error ID so we could cross reference it in the error log database.  I needed to replace this with the ELMAH version of the same thing.  It turns out to be simple, after I figured out which class to use:

System.Collections.IList errorList = 
    new System.Collections.ArrayList();
Elmah.ErrorLog.GetDefault(this.Context)
   .GetErrors(0, 1, errorList);

if(errorList.Count > 0)
{
 Elmah.ErrorLogEntry entry = 
             errorList[0] as Elmah.ErrorLogEntry;
 // do what you like with 'entry'
}

Thursday, February 23, 2012

ASP.NET MVC Error Handling Flowchart

In an effort to better understand ASP.NET MVC's error handling flow, I did some tests with various configurations of web.config settings and controller/action HandleError attribute decorations (as well as some reading on that type).  I think I understand it and thought I'd put together this flowchart to visualize the flow.  I hope it is of some use to whomever finds it.

ASP.NET MVC Error Handling Flowchart