Skip to content

Instantly share code, notes, and snippets.

@willnss
Created November 26, 2011 12:32
Show Gist options
  • Save willnss/1395579 to your computer and use it in GitHub Desktop.
Save willnss/1395579 to your computer and use it in GitHub Desktop.
Modificando o Layout/MasterPage via atributo no ASP.NET MVC
//MvcApplication1\Infrastructure\ChangeLayoutAttribute.cs
using System.Web.Mvc;
namespace MvcApplication1.Infrastructure
{
public class ChangeLayoutAttribute : ActionFilterAttribute, IResultFilter
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
var viewResult = filterContext.Result as ViewResult;
if (viewResult != null)
{
// realiza lógica para escolha do layout caso seja Razor ou masterpage caso seja ASPX
viewResult.MasterName = "AdminMaster";
}
}
}
}
//MvcApplication1\Controllers\AdminController .cs
namespace MvcApplication1.Controlllers
{
[ChangeLayout]
public class AdminController : Controller
{
//
// GET: /Admin/
//[ChangeLayout]
public ActionResult Index()
{
return View();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment