0

I have been researching dynamic content for MVC views and partial views but have not successfully found an architecture to fit my needs.

Basically I am required to create a landing page based on parameters pass by the URL. For basics http://mydns.com/myconroller/myview/?landingpage=Param1

The controller will need to find the HTML that will be used to create the view. The view is going to be different based on the landing page. (for the sake of the question, I am using landingpage as an example) My goal is to be able to deploy a Landing page and based on the URL use that HTML Landing page in the view based on the landingpage parameter that is passed.

There are other views that are working currently in the controller. I am trying to add functionality to be able to add a new one time page without having to recompile.

I have searched through various ideas on how to load dynamic views but cannot seem to find a solution that fits this need based on what I have read.

I can possibly RedirectToAction but I am still in the dark on where to deploy and I am getting several problems with Razor as it is not in the shared directory and then I am stuck with deployment issues as I want to organize the landing pages differently than I am organizing the views.

Solution: I decided to take a different approach and use the ContentResult Action in the controller. I still have the Main View and I use the HTML extensions to render the HTML pages that I have deployed in my customer's directory.

@{
  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 be used and it will be loaded into the View. The View can then house all of 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 any directory structure that I want. The configuration XML file allows me to find XML elements and use those values for any HTML that I am looking for, like a welcome and thank you page. The helper object will open the XML and find 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 duties and works independently on it own letting the raw HTML decorate the View. The model is still intact and can be used as I wish. The FormCollection is there in case a form submit posts the values to the view and provides some things that I omitted from this question as it did not pertain to this subject.

I don't want to answer my own question and I found the pieces that helped me on another site, so I am putting what I did here in case anyone needs this functionality.

2
  • Something like this perhaps? stackoverflow.com/questions/23813744/… Commented Jul 30, 2014 at 20:31
  • Thank you for the help, I decided to user the HTML extensions and load the files into the view that way. Commented Jul 31, 2014 at 21:02

1 Answer 1

1

This sounds like using the you can inherit from the virtual path provider view engine and decide based on the URL parameters (or other) which view to return. Some example 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 there you can return a RazorView or WebFormView and set your desired path for the view to use.

Sign up to request clarification or add additional context in comments.

3 Comments

Individual views did not work, but I liked the CustomViewEngine idea and used it for other things. I needed to deploy with a simple xCopy to a directory without creating new views or inheriting any MVC objects. The HTML that I get is just plain jane vanilla HTML with no functionality at all.
then you can return not a razor view but a custom view that does nothing but loading the plain html file and show it (or do some custom string replacement or whatever you want)
I decided to use a different approach as in the edited question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.