简体   繁体   中英

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?

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);
        ...
    }
}

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"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM