Picflight
Picflight

Reputation: 3852

What is the basepage equivalent in MVC

In my ASP.Net web sites I have the following code that I am able to use site-wide.
How do I do the same in ASP.Net MVC2?

public class BasePage : Page
{
 public BasePage()
 {
    this.PreInit += new EventHandler(BasePage_PreInit);
 }

 /// <summary>Every page executes this function before anything else.</summary>
 protected void BasePage_PreInit(object sender, EventArgs e)
 {
    // Apply Theme to page
    Page.Theme = "Default";
 }
 public bool IsSiteAdmin(string userName)
 {
    if (System.Web.Security.Roles.IsUserInRole(userName, "SiteAdmin1"))
        return true;
    return false;
 }
}

Upvotes: 2

Views: 3877

Answers (3)

Samantha Branham
Samantha Branham

Reputation: 7451

As zaph0d said, you want to override the Controller class. There are several "events" you can override when creating your own Controller class. A list of those would be here:

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller_members.aspx

Here's what you might want to do. Note that I have no idea what Page.Theme is or does.

public class BaseController : Controller
{
    protected string Theme { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext context)
    {
        Theme = "Default";
    }

    public bool IsSiteAdmin(string userName)
    {
        return System.Web.Security.Roles.IsUserInRole(userName, "SiteAdmin1");
    }
}

Upvotes: 6

Paul Hiles
Paul Hiles

Reputation: 9788

Not sure how themes fit into MVC (not very well I suspect), but in general you just need to create a base controller class.

public class BaseController : Controller

and then derive all your controllers off this base.

public class HomeController : BaseController

That way, you can have common functionality available to all controllers. eg your IsSiteAdmin method.

Upvotes: 7

No Refunds No Returns
No Refunds No Returns

Reputation: 8356

MVC has master pages and views. It sounds like you want your Controller to have some base logic in it instead of your page. In your controller you can select a different master page when rendering your views, based on your condition, if you want.

Upvotes: 3

Related Questions