Create dynamic views with static HTML in MVC


Robbye Rob

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.

Eleazar

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.

Related


Create dynamic views with static HTML in MVC

Robbye Rob 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, v

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Dynamic SQL to create views

Simon Paris I have a table with a list of store names that perform under par. Each store name has its own database on the server (we are in the process of merging all of them, but currently all of them are separate). Is it possible to iterate over the table (c

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Dynamic SQL to create views

Simon Paris I have a table with a list of store names that perform under par. Each store name has its own database on the server (we are in the process of merging all of them, but currently all of them are separate). Is it possible to iterate over the table (c

Create dynamic views in Android?

Teff In my application, I receive JSON multiple forms like this: "forms":[ {"name": "form1","title": "First Form","order": 1, "fields":[ "name": "firstName","type": "InputText", "maxLength": "10", "restriction

Create Views with Dynamic SQL

Mark Rooker I am trying to create a dynamic database creation script. There are a lot of steps and we often create this database so the script looks like this. DECLARE @databaseName nvarchar(100) = 'DatabaseName' EXEC('/*A lot of database creation code built

Dynamic SQL to create views

Simon Paris I have a table with a list of store names that perform under par. Each store name has its own database on the server (we are in the process of merging all of them, but currently all of them are separate). Is it possible to iterate over the table (c

Make (create) reusable dynamic views

Sane Developer The team wanted to create reusable, styleable views. For example, we want to reuse in different applications CommonPromptView(our own customizable dialog where we can hide the "Cancel" button, set a title, show a specific icon, etc.). There are

How to create dynamic views in Angular

lucky I want to dynamically create multiple horizontal lines using data obtained from a json file as shown in the image below. I try to do this component.html <div style="text-align: center"> <div class="flex-items"> <div *ngFor="let item of timelineObje

Django views and templates and static html

Phil O: I want to construct some HTML in a view and then render it in a template. What I see is rendered <div>xyz</div>, I just want to see xyz. What am I doing wrong? My template fragment: {{ normalized }} My view fragment: context["normalized"] = "<div>xyz<

Django views and templates and static html

Phil O: I want to construct some HTML in a view and then render it in a template. What I see is rendered <div>xyz</div>, I just want to see xyz. What am I doing wrong? My template fragment: {{ normalized }} My view fragment: context["normalized"] = "<div>xyz<

Django views and templates and static html

Phil O: I want to construct some HTML in a view and then render it in a template. What I see is rendered <div>xyz</div>, I just want to see xyz. What am I doing wrong? My template fragment: {{ normalized }} My view fragment: context["normalized"] = "<div>xyz<

Django views and templates and static html

Phil O: I want to construct some HTML in a view and then render it in a template. What I see is rendered <div>xyz</div>, I just want to see xyz. What am I doing wrong? My template fragment: {{ normalized }} My view fragment: context["normalized"] = "<div>xyz<

img src not working for static images in mvc views

sony In a view, I want to display an image stored in a view folder called Errorpage.png. Now I want to display the image in the Error.cshtml view file. this is my code <img src="~/Views/Errorpage.png" alt="error"> Now this code doesn't work and shows error al

Creating dynamic views in Odoo breaks MVC

username I've spent a while trying to figure out how to do conditional view inheritance, i.e. modify the view only if the condition is met. The inherited views are as follows: <record id="invoice_tree_inherit" model="ir.ui.view"> <field name="name">

Can I provide dynamic models for MVC views

legal photography I am developing a create form in a .Net MVC project. I will create a simple demo of this problem to get as close as possible to my actual business scenario. I have a human and several other classes that inherit from it. public class Person {

Creating dynamic views in Odoo breaks MVC

username I've spent a while trying to figure out how to do conditional view inheritance, i.e. only modify the view when the condition is met. The inherited views are as follows: <record id="invoice_tree_inherit" model="ir.ui.view"> <field name="name

Can I provide dynamic models for MVC views

legal photography I am developing a "create" form in a .Net MVC project. I will create a simple demo of this problem to get as close as possible to my actual business scenario. I have a human and several other classes that inherit from it. public class Person

Spring MVC dynamic views + handling non-existing views

inhibition I'll implement a Spring MVC controller that uses a dynamic view passed as a parameter for viewing: @Controller @RequestMapping("/page") public class PageController { @RequestMapping(value = "/{page}", method = {RequestMethod.GET}) public M

Spring MVC dynamic views + handling non-existing views

inhibition I'll implement a Spring MVC controller that uses a dynamic view passed as a parameter for viewing: @Controller @RequestMapping("/page") public class PageController { @RequestMapping(value = "/{page}", method = {RequestMethod.GET}) public M

Create dynamic views based on ngRoute and Restlike API

John MacDonald I have built a small HTML page with an ngView directive, after calling the Restlike API, the page is updated when any navigation link is clicked. This seems to work well, especially when there are no parameters to use in the request. As soon as

Create dynamic views based on ngRoute and Restlike API

John MacDonald I have built a small HTML page with an ngView directive, after calling the Restlike API, the page is updated when any navigation link is clicked. This seems to work well, especially when there are no parameters to use in the request. As soon as

iOS: Create multiple dynamic views in interface builder

username How to create a view with dynamic height based on constraints? Here is what I am trying to achieve: I set constraints for each view, butExcept for height , since height should be created dynamically . Is there a way to achieve this without writing cod