Get Aspx page content before sending to client

Posted on 2009-12-29
Last Modified: 2012-05-08
We have implemented themes in our web application by using attached files
and <link href="CSS.Aspx" rel="stylesheet" type="text/css" />

we stores themes in db and have used <% %> in CSS.Aspx  
depending upon the user themes settings is fetched from DB and appropriate variables are assigned values to the variables like 'TextBoxColor' in attached files.

everything works fine.

Now I need to export this dynamic css file or in simple words i need to save this CSS.Aspx on server side after <% %> is replaced by its value (file similar to attached RenderedCSS.css ) so that i can zip it and return to the user
I have used attached code but problem with this code is that it gives me CSS.Aspx file with <% %> but i want file similar to RenderedCSS.css

also i'm using .net 2.0 framework

P.S. Don't suggest me to change my approach of dynamic CSS(CSS.Aspx) with App_Themes approach  because its not possible to change this in our project.
String inputFile = MapPath("../css/css.aspx");
        String sDiskFile = "../temp/result.css";

        WebRequest webRequest = WebRequest.Create(inputFile);

        WebResponse webResponse = webRequest.GetResponse();
        Stream stream = webResponse.GetResponseStream();

        StreamReader oReader = new StreamReader(stream);

        StreamWriter oWriter = new StreamWriter(Server.MapPath(sDiskFile));


Open in new window

Question by:Rakesh Jaimini
    LVL 2

    Expert Comment

    hey rakeshjaimini, what'd i'd probably do in your case is make the contents of css.aspx rendered by a custom control. This is what i'd try:

    1) create a class called CustomCSS that inherits from Control

    namespace app
      public class CustomCSS : Control
        protected override void Render(HtmlTextWriter writer)
           writer.Write(".fieldvalue { text-align: left; font-family: \"areal\"; }");
           writer.Write(".radiobtn { font-family:\"arial\"; font-size: 10px;");
            // continue writing out the rest of your css file.
    2) compile this control into a .dll named mycontrol
    3) then in your web.config file place the following tag:
    <add tagPrefix="MyControls" namespace="app" assembly="mycontrols" />

    4) then your Css.aspx file should look this

    <%@ Page Language="C#" Async="false" Trace="false">
             <MyControls:CustomCSS ID="css" runat="server" />

    since all of the content for this page comes from your control you can simply call the Render method to get the contents. Like this:

    StringWriter stringwriter = new StringWriter();
    HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);

    CustomCSS _customcss = new CustomCSS();

    string s = stringwriter.ToString();

    I'm not sure if i answered your question or not, but hopefully that helps.

    LVL 11

    Accepted Solution

    I wonder if the result would be any different if you change your request path...

    *** Change this line from ***
    WebRequest webRequest = WebRequest.Create(inputFile);  // which is now "../css/css.aspx"

    *** to ***
    WebRequest webRequest = WebRequest.Create("");

    I *think* that should do it since it will actually come through the webserver and over port 80, rather than through the file system.
    LVL 10

    Author Comment

    by:Rakesh Jaimini
    nice thinking and surely approach will work but in my case this will require lot of rework,also if i create control then i need to create 4 5 controls as we supports all browsers
    and have made separate files for each (IECSS.Aspx,FireFoxCSS.Aspx).
    And most important i need to teach everyone in my team for this new approach which is most difficult job
    so i'm dropping you approach :)

    i have used "https://rakesh939/appname/css/css.aspx" but now i'm getting
    "The remote certificate is invalid according to the validation procedure." exception
    then i tried using http and put my file out of authorization and it worked

    as our servers are running on https how can i remove exception while using https
    LVL 11

    Expert Comment

    Question for you: is https://rakesh939/appname/css/css.aspx the full domain name? It needs to match what is listed in the certificate or you will most likely get the error.

    These posts may help figure it out too...
    -- This one will probably fix the problem if the fully qualified domain name doesn't get rid of the exception.
    -- Look for the post by JohnAbot.
    -- I assume you stick the handler call in the constructor of the class/page that makes the call to the service.
    -- This post confirms what's said in the second one.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now