Create dynamic views with static HTML in MVC
I've been looking into dynamic content for MVC views and partial views but have not been successful in finding an architecture that suits my needs.
Basically, I need to create a login page based on the parameters passed by the URL. For the basics, visit http://mydns.com/myconroller/myview/?landingpage=Param1
The controller will need to find the HTML that will be used to create the view. Depending on the landing page, the view will be different. (I'm using a login page as an example for the sake of the question) My goal is to be able to deploy a login page and use that HTML login page in a view based on the URL based on the login page parameters passed.
There are other views currently active in the controller. I'm trying to add functionality so that a new page of pages can be added without recompiling.
I've searched various ideas on how to load dynamic views, but can't seem to find a solution for this need based on what I've read.
I might be able to use RedirectToAction, but I still don't know where to deploy, I ran into some issues since Razor wasn't in a shared directory, and then I ran into deploys since I wanted to organize the landing pages differently than I did The question is sorting out comments.
Solution: I decided to go the other way and use ContentResult Action in the controller. I still have the main view and I use the HTML extension to render the HTML pages that have been deployed in the customer catalog.
@{
Html.RenderAction("LandingPageContent", "Controller", Model);
}
Then in the controller, I load the HTML directly and return the ContentResult
public ContentResult LandingPageContent(object model, FormCollection collection)
{
MySRCHelper helper = new MySRCHelper();
ContentVariables variables = helper.getContentSRC(model.EntryCode);
model.ContentSRC = variables.LandingPageSRC;
return Content(System.IO.File.ReadAllText(Server.MapPath(model.ContentSRC)));
}
I can then configure the path to the raw HTML file to use and load it into the view. The View can then hold all the paths to load jQuery, CSS and other necessary javascript to integrate with the raw HTML and allow me to deploy the HTML files into whatever directory structure I want. The configuration XML file allows me to look up XML elements and use those values for whatever HTML I'm looking for, such as a "welcome and thank you page". The helper object will open the XML and look up the configuration based on the parameters passed to the View.
<ContentLandingItem entrycode="1" customerID="Cutomer1">
<ContentLandingPageSRC>~/Customers/Customer1/Customer1Landing.htm</ContentLandingPageSRC>
<ContentThankyouSRC>~/Content/Default/GenericThankyou.htm</ContentThankyouSRC>
</ContentLandingItem>
<ContentLandingItem entrycode="2" customerID="Cutomer2">
<ContentLandingPageSRC>~/Customers/Customer2/Customer2Landing.htm</ContentLandingPageSRC>
<ContentThankyouSRC>~/Customers/Customer2/Customer2Thankyou.htm</ContentThankyouSRC>
</ContentLandingItem>
The view still performs its responsibilities and operates independently, letting the raw HTML decorate the view. The model is still intact and can be used as needed. If there is a form submitter that submits the value to the view and provides something that I've omitted in this question, that FormCollection can solve it because it's not relevant to the topic.
I don't want to answer my own question, I found works on other sites that helped me, so if anyone needs this, I'll explain it here.
It sounds like using, you can inherit from the virtual path provider view engine and determine which view to return based on URL parameters (or whatever). Some examples that you can adjust to your needs:
public class CustomViewEngine : VirtualPathProviderViewEngine
{
public MyViewEngine()
{
this.ViewLocationFormats = new string[] { "~/Views/{1}/{2}.mytheme ", "~/Views/Shared/{2}.mytheme" };
this.PartialViewLocationFormats = new string[] { "~/Views/{1}/{2}.mytheme ", "~/Views/Shared/{2}. mytheme " };
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
var physicalpath = controllerContext.HttpContext.Server.MapPath(partialPath);
return new RazorView(controllerContext, physicalpath);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
var physicalpath = controllerContext.HttpContext.Server.MapPath(viewPath);
return new RazorView(controllerContext, physicalpath);
}
}
In it, you can go back to the RazorView or WebFormView and set the desired view usage path.