Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can .NET intercept and change css files?

UPDATE 1:

I have now setup IIS6 so .NET can handle calls to .css files. What do I have to do now to get it to change css files based on the referal url? So if the referal url is http://intranet/, it should continue calling the old style sheets. If the referal url is http://intranetv2/, it should call the new style sheet.


ORIGINAL QUESTION:

The background:

I have 2 search engines. 1 is old and the other is brand new (development stage). Search engine 1 is on domain1.com and search engine 2 is on domain2.com. Both domains are on the same server. Both search for webpages on domain1.com based on what the user enters into the search engines. The difference between the 2 search engines is that the new one is faster, produces more accurate results, and has a hugely improved user interface. Both search engines will remain live just so the users can get used to the new one in their own time, instead of just throwing them into the deep end and removing the old search engine altogether.

Anyway, enough of the background, basically, as the searchable pages reside on the old domain name with the old search engine, whereas the new search engine is on the new domain name, ...

The question:

... can I use HttpModule, or another part of .NET, or something from IIS6 even to capture the page links generated by the new search engine, and dynamically change the css file attached to the old searchable pages on the old domain?

The reason:

In effect making it look like a full brand new site, where if the search engine on the old domain is used to access the pages on the old domain, the old stylesheet is used, but if the search engine on the new domain is used to access the searchable files on the old domain name, a new stylesheet should be used to make the old pages look new. As there are lots of searchable pages, in the region of 10,000, editing each and every page to add an if statement to check the referral domain name before adding a style sheet to the pages is not a realistic option.

The environment:

The old search engine, along with the searchable pages on the old domain use .net 1.something, but the new search engine on the new domain name is using .net 3.5, and I am using vb.net for the asp.net pages. The server is a IIS6 server.

like image 982
oshirowanen Avatar asked Jun 01 '11 11:06

oshirowanen


2 Answers

Yes, you should be able to use an HttpModule on your "old" application to intercept the calls to the old CSS. Based on the version of IIS you have on your server, you may need to do some configuration to make sure .NET is handling calls to .css files, otherwise your HttpModule would not be called. See this question for reference.

Once .NET is handling calls to CSS, you can dynamically switch the css dynamically in case the request is for the "old" css file.

Here's an old article (since you are on .NET 1.1) that should point you in the right direction for the implementation and the configuration of IIS: "URL Rewriting in ASP.NET". Basically what you are doing is very similar, since you are "rewriting" a specific URL (the one to your old CSS file) to point to different content.

like image 23
Paolo Falabella Avatar answered Oct 05 '22 03:10

Paolo Falabella


  1. In IIS, Setup the HttpHandler to receive all the file types you want (says you have done this)
  2. user Server.MapPath() on HttpRequest.Url.AbsolutePath to get the physical path
  3. Modify the path according to the domain
  4. Write the file to the response stream.

Here is a handler (simplified) that I use routinely to server alternate files for different domains:

using System;
using System.IO;
using System.Web;
public class MultiDomainFileHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string filePath = GetDomainSpecificFilePath(context.Request.Url.Host,
            context.Server.MapPath(context.Request.Url.AbsolutePath));

        if (File.Exists(filePath))
        {
            switch (Path.GetExtension(filePath).ToLower())
            {
                case ".css": context.Response.ContentType = "text/css"; break;
                case ".jpg":
                case ".jpeg": context.Response.ContentType = "image/jpeg"; break;
                //other types you want to handle
                default: context.Request.ContentType = "application/octet-stream"; break;
            }
            context.Response.WriteFile(filePath); //Write the file to response
        }
        else context.Response.StatusCode = 404;
    }

    private string GetDomainSpecificFilePath(string domain, string originalPath)
    {
        string prefix = "";
        switch (domain.ToLower())
        {
            case "intranetv2": prefix = FILE_PREFIX_INTRANETV2; break;
            case "www.example.com": prefix = FILE_PREFIX_EXAMPLE_DOT_COM; break;
            //other domains you want to handle
        }
        string dir = Path.GetDirectoryName(originalPath);
        string fileName = prefix + Path.GetFileName(originalPath);
        return Path.Combine(dir, fileName);
    }

    const string FILE_PREFIX_INTRANETV2 = "v2.", FILE_PREFIX_EXAMPLE_DOT_COM = "ex.com.";
    public bool IsReusable { get { return false; } }
}

Now, you simple need to have alternate files in the same directories. E.g:

/Images/logo.jpg

/Images/v2.logo.jpg

/Styles/mystyle.css

/Styles/v2.mystyle.css

I hope this helps :)

like image 69
Vaibhav Garg Avatar answered Oct 05 '22 03:10

Vaibhav Garg