Hi, I have what seems like a difficult problem to get around, but I'm sure it can be done. I have a requirement to create dynamically managed content in an MVC4 application in C #. I understand how to create my controllers and map them to views, but I canโt figure out how to do this dynamically. The following is the scenario:
The user goes to the CMS and creates a page of a virtual type in a new folder in the root of the site. This folder and page do not exist in my MVC application. The URL to access the new page will be similar to www.newfolder / newpage. I have a full page view of the page that the user selects as a view to load from a new page.
So my question is how to create an MVC controller and an action that can take the url above and provide me with the correct look and keep the dynamic structure of the url. I read a few things, but it really doesn't look like what I'm trying to do.
Any help would be great.
Greetings
Update 09/29/2015 For those who comment below, to ask how this was done at the end, the code below is for my route configuration and my dynamic controller.
Route configuration
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 } ); } }
My dynamic page controller
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"); } } } }
Obviously, there is more code in my service layers and in my database for dynamic content, but I can share it if it helps anyone.
thanks