Created
November 26, 2011 12:32
-
-
Save willnss/1395579 to your computer and use it in GitHub Desktop.
Modificando o Layout/MasterPage via atributo no ASP.NET MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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