Iternity
Iternity

Reputation: 892

How to implement multiple templates of home page together in ASP.NET MVC c#?

I have 4 different templates of a home page[ with different data's from the database]. I would like change periodically from one to another from some sort of back-end admin access. I have created 4 index(index1.aspx,index2.aspx..) pages. what would be the easiest way to change the default index page from time to time.

I imagine doing that by creating a config file and modifying that.. is there any better way of doing that?

Upvotes: 1

Views: 811

Answers (2)

chandmk
chandmk

Reputation: 3481

Create a separate file for storing appsettings. Add a setting for your current home page.

Store this setting in external config file.

  <appSettings file="appSettings.config">
     Your other settings
  </appSettings>

And your appSettings.config

  <appSettings>
     <add key="currentHomePage" value="HomePage1"/>
  </appSettings>

You can programatically modify these settings, which you would do for your admin users.

Here is a link that shows how to do this

Now in your controller action, you can simply retrieve the current home page from the app settings.

string currentHomePage = WebConfigurationManager.AppSettings["currentHomePage"];

Upvotes: 1

Kirk Woll
Kirk Woll

Reputation: 77546

I assume you have a controller action like:

public ActionResult Index() 
{
    return View();  // Implies Index.aspx
}

You probably just want to swap out views:

public ActionResult Index() 
{
    HomePageType homePageType = GetHomePageTypeFromDb();
    HomePageModel model = new HomePageModel();

    switch (homePageType) 
    {
        case Type1: return View("Index1", model);
        case Type2: return View("Index2", model);
        ...
    }
}

Upvotes: 3

Related Questions