Get Aspx page content before sending to client

Posted on 2009-12-29
Medium Priority
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
  • 2

Expert Comment

ID: 26140994
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

Kelvin McDaniel earned 2000 total points
ID: 26141763
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("http://theRealAddress.com/css/css.aspx");

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
ID: 26143684
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

by:Kelvin McDaniel
ID: 26144788
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses

862 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