Skip to content

Instantly share code, notes, and snippets.

@Grinderofl
Created October 27, 2011 09:20
Show Gist options
  • Save Grinderofl/1319139 to your computer and use it in GitHub Desktop.
Save Grinderofl/1319139 to your computer and use it in GitHub Desktop.
C# .NET: MultiViewController for MVC3
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MultiViewController.cs" company="">
// Nero Sule
// </copyright>
// <summary>
// Defines the MultiViewController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Web.Infrastructure
{
using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Norstat.Domain.Library;
using Norstat.Log;
/// <summary>
/// Multi-View Controller
/// Usage:
/// Controller/Action/1 - HTML based on route
/// Controller/Action.html/1 - HTML
/// Controller/Action.json/1 - JSON
/// Controller/Action.xml/1 - XML
/// Controller/Action.partial/1 - partial view of action name
/// Controller/Action.clean/1 - partial html without styling
/// Needs routes specified in global.asax
/// For example:
/// Routes.MapRoute(null, "{controller}/{action}.{format}");
/// Routes.MapRoute(null, "{controller}/{action}.{format}/{id}");
/// </summary>
public abstract class MultiViewController : BaseController
{
#region Constants and Fields
/// <summary>
/// Formatting key
/// </summary>
private const string FormatKey = "format";
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="MultiViewController"/> class.
/// </summary>
protected MultiViewController()
{
this.RequestedFormat = FileFormat.Html;
}
#endregion
#region Enums
/// <summary>
/// Possible file format
/// </summary>
public enum FileFormat
{
/// <summary>
/// HTML type
/// </summary>
Html,
/// <summary>
/// JSON type
/// </summary>
Json,
/// <summary>
/// XML type
/// </summary>
Xml,
/// <summary>
/// Partial type
/// </summary>
Partial,
/// <summary>
/// Empty layout
/// </summary>
Clean
}
#endregion
#region Properties
/// <summary>
/// Gets RequestedFormat.
/// </summary>
protected FileFormat RequestedFormat { get; private set; }
#endregion
#region Methods
/// <summary>
/// Formats view
/// </summary>
/// <param name="viewName">
/// The view name.
/// </param>
/// <param name="viewModel">
/// The view model.
/// </param>
/// <returns>
/// Formatted view
/// </returns>
/// <exception cref="FormatException">
/// Invalid format
/// </exception>
protected ActionResult FormatView(string viewName, object viewModel)
{
switch (this.RequestedFormat)
{
case FileFormat.Html:
if (viewName != string.Empty)
{
return View(viewName, viewModel);
}
return View(viewModel);
case FileFormat.Json:
return this.Json(viewModel);
case FileFormat.Xml:
return new XmlResult(viewModel);
case FileFormat.Partial:
return this.PartialView(this.ControllerContext.RouteData.Values["action"] + "Partial", viewModel);
case FileFormat.Clean:
if (viewName != string.Empty)
{
return this.View(viewName, "~/Views/Shared/_Clean.master", viewModel);
}
ViewResult v = View(viewModel);
v.MasterName = "~/Views/Shared/_Clean.Master";
return v;
default:
throw new FormatException(
string.Concat("Cannot serve the content in the request format: ", this.RequestedFormat));
}
}
/// <summary>
/// Formats view
/// </summary>
/// <param name="viewModel">
/// The view model.
/// </param>
/// <returns>
/// Formatted view
/// </returns>
protected ActionResult FormatView(object viewModel)
{
return this.FormatView(string.Empty, viewModel);
}
/// <summary>
/// Action is being executed
/// </summary>
/// <param name="filterContext">
/// The filter context.
/// </param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
RouteValueDictionary routeValues2 = filterContext.RouteData.Values;
base.OnActionExecuting(filterContext);
RouteValueDictionary routeValues = filterContext.RouteData.Values;
if (routeValues.ContainsKey(FormatKey))
{
string requestedFormat = routeValues[FormatKey].ToString();
if (IsValidFormat(requestedFormat))
{
this.RequestedFormat = (FileFormat)Enum.Parse(typeof(FileFormat), requestedFormat, true);
}
}
}
/// <summary>
/// Checks if the format is valid
/// </summary>
/// <param name="requestedFormat">
/// The requested format.
/// </param>
/// <returns>
/// Is format valid
/// </returns>
private static bool IsValidFormat(string requestedFormat)
{
return Enum.GetNames(typeof(FileFormat)).Any(format => format.ToLower() == requestedFormat.ToLower());
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment