Get Aspx page content before sending to client

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

LVL 10
Rakesh JaiminiAsked:
Who is Participating?
Kelvin McDanielConnect With a Mentor Sr. Developer/ConsultantCommented:
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.
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.

Rakesh JaiminiAuthor Commented:
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
Kelvin McDanielSr. Developer/ConsultantCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.