2

Ok, I'm still new to Asp.Net and MVC3. I'm becoming more familiar with things but still experimenting after finishing my first web application (a simple web form submission into a database). Now I am working on smaller projects such as converting some old ColdFusion web forms that submit e-mails. I've easily accomplished this in MVC3 but all of our web pages are in a separate content management system where our central HTML template is. I've already asked a question about this here and didn't get anywhere.

What we have is the majority of our web pages get exported from the CMS as straight HTML files, and only the ones that need database access or a programming language are exported as ColdFusion. It's very easy to "include" ColdFusion code to use inside of the template in our CMS. I would love to be able to use this HTML template in my mvc3 project but I've found no way to perform an "include" or link to an external file. I'm not sure how this would work anyway, so I settled on just copy/pasting the template to mvc3 and figuring out a way I can share this template (now a "layout") between all of the small projects I'll be working on. If the template changes I do not want to have to update every single little mvc3 web application. I learned about using "Areas" but it seems you can't just publish a single area to a folder on the web server, the whole project has to be deployed.

All I really need is a way for small mvc3 projects to use one template and these small mvc3 projects to be scattered all over our web server. Would this best be done in one large project that publishes to multiple different folders, or as many small projects that can share a common layout? Is either of these two possible?

After attempting and experimenting with all of this, I'm beginning to think MVC is not going to work with what I want. It seems better suited for intranet applications or entire web sites, not this little "here and there" applications like what I want. Should I learn Web Forms instead? I know I can "include" a aspx file inside our CMS much like I do with ColdFusion.

Community
  • 1
  • 1
  • So, you want some kind of static content that can be accessed by _multiple_ disconnected sites? – Tejs May 18 '12 at 18:54
  • Ideally, yes. Something like "including" a top navigation XML would help, but being able to split the entire html layout into not-well-formed chunks that I can update centrally somewhere would be best. If this isn't possible through MVC though, I understand that Web Forms might be the best way to go since I already know I **can** do this with aspx files. – Vince Ruppert May 18 '12 at 19:06
  • WebForms isnt going to function any differently than MVC in this regard. Whatever is possible is WebForms is also possible in MVC. – Tejs May 18 '12 at 19:07

1 Answers1

0

Do your templates have to be "exported" from the CMS? Or, can you have a template that "lives" on a static CMS URL? This is what we do for apps that need db access / can't be easily done within the CMS, but need to share the same look and feel.

What you can do is have your plain old HTML file live at a URL, for example, https://cms.domain.tld/templates/designxyz.html. That file will serve up a basic layout, except where your custom app content goes, you simply have the string "content goes here".

Then, from the MVC app, you can call this URL to get the HTML content as a string. Once you have the string, you can split it in 2 before and after the "content goes here" string. Then, in your layout.cshtml file, you can do something like this:

@{
    const string contentPlaceholder = "content goes here";
    var allHtml = GetHtmlTemplateFromLiveServer();
    var index = allHtml.IndexOf(contentPlaceholder);
    var topHtml = allHtml.Substring(0, index);
    var botHtml = allHtml.Substring(index + contentPlaceHolder.Length);
}
@topHtml
@RenderBody()
@botHtml

If something like this works, you can then abstract all of this away into a HTML Helper, then reuse that helper in other projects (NuGet would be good for this).

_Layout.cshtml

@{
    var options = new CmsTemplateRenderOptions
    {
        Url = "https://cms.domain.tld/templates/designxyz.html",
        Cache = new TimeSpan(1, 0, 0);
    };
}
@Html.RenderCmsTemplate(CmsTemplateRenderRegion.Top, options)
@RenderBody()
@Html.RenderCmsTemplate(CmsTemplateRenderRegion.Bottom, options)

Then, to update the layout for all of your apps, you would just publish changes to the https://cms.domain.tld/templates/designxyz.html URL.

danludwig
  • 46,965
  • 25
  • 159
  • 237
  • really? reusing layouts across projects seems to be a valid requirement IMHO. But this way makes it truly difficult, and counter-scalable. Each time a view is to be shown, a URL has to be called. – Saeed Neamati May 15 '16 at 05:02