Precompilation of aspx and cshtml pages, but leaving the master or layout pages updateable
Asked Answered
G

1

19

Not sure if what I am after here is even achievable with the aspnet_compiler.exe tool, but here goes:

We have a site that we allow users to skin by allowing them to modify master pages - or rather, we don't allow them to modify them directly, we give them a cut down "markup" style language that they can use to modify the html of those pages, so they can essentially "skin" the site to look like their own front end.

I'm keen to try to optimise the site by precompiling the views and the aspx pages. But I want the layout and master pages that those views and pages USE to remain updatable. However, I can't get that to work...

Say we have the following aspx page:

<%@ Page Title="Hello World" MasterPageFile="~/Skinable/Skin.Master" Language="C#" AutoEventWireup="true" CodeBehind="HelloWorld.aspx.cs" Inherits="Project.HelloWorld" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Content" Runat="server">
    Hello World!
</asp:Content>

Which references the master page file Skin.Master in the Skinable folder. Skin.Master needs to be updatable. Here is what I have tried:

  • If I simply compile the site using this aspnet_compiler.exe command, the master page gets compiled too, and is not updatable:

    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe -v / -p d:\SourceFolder d:\CompiledSite

  • If I EXCLUDE the skinable folder (adding -x Skinable to the command) in the aspnet_compiler.exe then the folder does not get copied to the output folder, but the master page is still compiled to the bin folder (ie, a skin.master.compiled file gets generated into the build folder). And when we copy the master page in to the deployment folder, changes to that file get ignored (it just used the master page as it was at compile time).

  • If I hide the Skinable folder before running aspnet_compiler (attrib +h d:\SourceFolder\Skinable) then I get the same result as above (ie when excluding via the -x flag)

  • If I specify the -u flag (Updatable) when running aspnet_compiler.exe, well, I'm not sure what the advantage of that is to be honest, as nothing seems to get compiled in that instance, so compilation takes place on the fly when you request a page, so what's the point...

  • Finally, if I allow compilation of everything, and then go into the bin folder and DELETE skin.master.compiled, then when you request a page that uses that master page, you get the error "The file 'Skin.master' has not been pre-compiled, and cannot be requested"

I suspect that the reference to the master page is the problem here - ie, if a page references a master page, then that master page MUST be compiled too if you want to compile the page, but I'm not sure. Am I on a hiding to nothing here?

Gainor answered 11/7, 2018 at 12:5 Comment(6)
You can recompile with updatable user interface flag -u:updatable learn.microsoft.com/en-us/aspnet/web-forms/overview/…. Solves half of your problem.Jutland
Thanks @DipenShah. Compiling with the -u flag seems to leave the assets updateable, which is great, but it doesn't seem to actually compile the assets so they can be loaded on the server without requiring compilation. Or am I missing something here? -u seems to be just a compilation check to ensure no runtime errors are encountered? Or again, am I missing something?Gainor
-u flag doesn't compile aspx files, it just compiles all code behind files so your aspx files will be compiled on request. That is why I said it's just half solution to your problemJutland
What about having a source web app, where everything is updateable, but only the whole precompiled web app gets deployed. If the admin changes the skin, he changes the skin in the source web app and the modified source web app gets precompiled and deployed again...?! Could something like this work?Cinda
But how will you prevent users form running C# code inside the master page code?Norman
@Gainor I had something similar back last year. Where users were allowed to create their own site with a custom template and their own names in the url. Have you tried changing the CodeBehind attribute of the markup file to CodeFile attribute? This will not compile the back-end file of that specific aspx page and you would be able to edit the files as well.Wiburg
D
1

What if you went about it a different way? What if you 'captured' their html and css changes and then made a new css folder or file for that company? (file is the original(s) plus their changes. Then when the master page is loaded, it pulls from a database what file or folder the company uses and writes that into the css from the master page's c#?

Process would be like:

  • Capture their changes
  • Make a css folder and files just like the original(s) but with their changes
  • Pull from database table of which css file to use.(you'll need to make a mapping table of client->css file)
  • Use Master page c# to write to the html which css file to use.

In essence, this makes the css changes loaded at runtime based on your client and you don't have to worry about the compilation.

We used this process successfully for skinning different colors. Our folder system was like:

  • Default -> Original Css Files
  • Dark theme -> Original with dark changes
  • Blue theme
  • Pink theme etc
Downbeat answered 12/9, 2018 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.