title: Create Dynamic MVC Controllers, Actions for Dynamic Pages
body: <p>Hi I have what seems to be a comlex issue to get around but I'm sure it can be done. I have the requirement to create a dynamically content managed MVC4 application in C#. I understand how to go about creating my controllers and mapping them to views but what I can't figure out is how to do this dynamically. The scenario is below:</p>
  
  <p>A user goes into the CMS and creates a virtual type page in a new folder within the website root. This folder and page do not exist in my MVC application. The URL to access the new page would be something like website.com/newfolder/newpage. I have a fullwidth page view that the user selects as the view to load with the new page.</p>
  
  <p>So my question is how do I create an MVC controller and action that can take the url above and deliver me the correct view and keep the url structure dynamically. I have read a few things but they don't really seem to be what I'm trying to do.</p>
  
  <p>Any help would be great.</p>
  
  <p>Cheers</p>
  
  <p><strong>Update 29/09/2015</strong>
  For those commenting below to ask how this was done in the end the code is below for my route config and my dynamic controller.</p>
  
  <p>Route Config</p>
  
  <pre><code>    public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
          routes.MapRoute(
              name:"Gallery",
              url: System.Configuration.ConfigurationManager.AppSettings["gallerycontroller"] + "{id}", 
              defaults: new { controller = "Component", action = "Gallery", id = UrlParameter.Optional });
  
          routes.MapRoute(
              name: "ContactForm",
              url: System.Configuration.ConfigurationManager.AppSettings["contactcontroller"] + "{action}/{id}",
              defaults: new { controller = "ContactUs", action = "ContactForm", id = UrlParameter.Optional });
  
          routes.MapRoute(
              name: "Dynamic", url: "{*page}", defaults: new { controller = "Master", action = "Load", page = UrlParameter.Optional}
              );
  
          routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
          );
      }
  }
  </code></pre>
  
  <p>My Dynamic Page controller</p>
  
  <pre><code>    public class MasterController : Controller
  {
  
      public ActionResult Load(string page)
      {
          if (page == null)
          {
              //load default page
              ContentDelivery _cd = new ContentDelivery(System.Configuration.ConfigurationManager.AppSettings["ContentDelivery"]);
              ContentPage _cp = _cd.GetPage(1);
              if (_cp != null)
              {
                  string _view = "PageViews/" + _cp.PageView;
                  return View(_view, _cp);
              }
              else
              {
                  return View("PageViews/404Page");
              }
          }
          else
          {
              ContentDelivery _cd = new ContentDelivery(System.Configuration.ConfigurationManager.AppSettings["ContentDelivery"]);
              ContentPage _cp = _cd.GetPage(page);
              if (_cp != null)
              {
                  string _view = "PageViews/" + _cp.PageView;
                  return View(_view, _cp);
              }else{
                  return View("PageViews/404Page");
              }
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>Obviously there is more code going on in my service layers and in my database for the dynamic content but I can share this as well if it is of help for anyone.</p>
  
  <p>Thanks</p>