ASP.NET MVC - Configuring the main view using a controller

Be that as it may, is there an easy way to set MasterView as the default for all actions inside a particular controller?

For example, if I have a HomeController , I want all the actions inside it to inherit Site.Master by default, but if I'm inside the AccountsController I want all the actions to inherit Admin.Master , etc.

I managed to do this with

return View("viewName", "masterName", objectModel);

But in doing this, I have to apply it every time I call the View method.

I was looking for something simpler, like on rails, where we can state:

class HomeController < ApplicationController

  layout 'site'

   def index
   end

   def create
   ...

end

class AccountsController < ApplicationController

  layout 'admin'

  def index
  end

  def create
  ...  

end

Is it possible?

Thank you in advance

+5
1

OnActionExecuting .

protected override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    ViewData["MasterfileToUser"] = "site";
}       

, , ActionFilterAttribute,

using System;
using System.Web.Mvc;
public class MasterFileFilterAttribute : ActionFilterAttribute
{
    public string Master { get; set; }

    public override void OnActionExecuted( ActionExecutedContext filterContext)   
    {        
            if (filterContext.Result is ViewResult)
                    ((ViewResult)filterContext.Result).MasterName = Master;
    }
}

, , :

[MasterFileFilterAttribute(Master = "site")] 
public class HomeController : Controller 
{ 
    // Action methods 
}   
+6

All Articles