Forcing MVC3 into an area

I had a bit of an issue today with an area in my MVC3 application. I added it with the standard tooling, but when I tried to navigate to it, I got some 404s and some 500s that the View couldn’t be found. It was looking for the View in the main application’s Views, not in my Area!

Well, there isn’t anything new under the sun and I found an answer on StackOverflow. I wasn’t quite satisfied with throwing that snippet in all of my actions, so I thought, “Why not an ActionFilter!?”

And so a new Github repository of ASP.NET MVC3 goodies was born! I am sure I’ll throw a lot more goodies in here, but for now, I give you AreaAttribute.cs.

/// <summary>
/// Signals that a Controller or Action belongs in the given Area when the MVC Framework
/// might miss it despite area registration
/// </summary>
/// <remarks>Based on http://stackoverflow.com/questions/5128009/mvc-3-area-route-not-working</remarks>
/// <example>
/// <![CDATA[
///     [Area("MyArea")]
///     public class MyAreaController : Controller
///     {
///         // ...
///     }
/// ]]>
/// </example>
public class AreaAttribute : ActionFilterAttribute
{
	private readonly string _areaName;
	private const string DataTokenKey = "area";

	/// <summary>
	/// Constructs a new AreaAttribute with the given areaName
	/// </summary>
	/// <param name="areaName">The target area</param>
	public AreaAttribute(string areaName)
	{
		_areaName = areaName;
	}

	/// <summary>
	/// Forces us into the target area if MVC Framework hasn't already figured it out
	/// </summary>
	/// <param name="filterContext">The executing context</param>
	public override void OnActionExecuting(ActionExecutingContext filterContext)
	{
		base.OnActionExecuting(filterContext);

		var controllerContext = filterContext.Controller.ControllerContext;
		if (!controllerContext.RouteData.DataTokens.ContainsKey(DataTokenKey))
		{
			controllerContext.RouteData.DataTokens.Add(DataTokenKey, _areaName);
		}
	}
}
Advertisements

About David Ruttka

I've been "making computers do things" since I first saw King's Quest on a 286 PC in the mid-80's, but I turned it into a career just over a decade ago. While the majority of my experience has been on the Microsoft stack (C#, .NET, ASP.NET), I've recently been diving deeper into JavaScript and exploring the Ruby universe. Occasionally, I'll do a public speaking gig or write a blog post. When I'm not coding, I enjoy spending time with my family, watching hockey, and playing the occasional video game. You can also find me on Stack Overflow, Google Plus, and Twitter. Microsoft Certified Programming in HTML5 with JavaScript and CSS3 Specialist; MCPD Windows Developer 3.5
This entry was posted in Development and tagged , , . Bookmark the permalink.

Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s