Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi all,
 
I have created a web application with URL Rewriting. It's working absolutely fine on localhost. But when uploaded to production Server it stopped working.
 
Then I read some where that extentionless URLs are not suppoeted in IIS6 (or IIS configuration needs to be changed, and for that I have no control). Therefore I put .aspx extention. And it start working (both locally & on Production server). But now on Production server CSS & JavaScript Files are not loading but on localhost its working correctly.
 
I am using Global.asax & my own module for URL Rewriting.
 
Code on Global.asax file:
void Application_AcquireRequestState(object sender, EventArgs e)
{
    //Session is Available here
    string strLastURL = "";
    string fullOrigionalpath = HttpContext.Current.Request.Url.ToString().ToLower();
 
    if (fullOrigionalpath.Contains(".aspx")  && !fullOrigionalpath.Contains("404error.aspx"))
    {//Only .aspx files to be processed

        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            if (HttpContext.Current.Session["LastAccessURL"] != null)
                strLastURL = HttpContext.Current.Session["LastAccessURL"].ToString();
            else
                HttpContext.Current.Session["LastAccessURL"] = strLastURL = fullOrigionalpath;
        }
 
        if (fullOrigionalpath.Contains("/example.aspx") && fullOrigionalpath != strLastURL)
        {
            HttpContext.Current.Session["LastAccessURL"] = fullOrigionalpath;
            HttpContext.Current.Response.Redirect("~/ChangedByUrl/Example.aspx");
        }
 
        try
        {
            HttpContext.Current.Session["LastAccessURL"] = fullOrigionalpath;
        }
        catch { }
    }
}
 

 

Rewrite Module Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
/// <summary>
/// Summary description for RewriteURL
/// </summary>
public class RewriteURL : IHttpModule
{
    public RewriteURL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
 
    public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }
 
    public void Dispose()
    {
    }
 
    private void Application_Disposed(Object source, EventArgs e)
    {
 
    }
 
    void Application_BeginRequest(object sender, EventArgs e)
    {
        string fullOrigionalpath = HttpContext.Current.Request.Url.ToString().ToLower();
 
        if (fullOrigionalpath.EndsWith("/changedbyurl/example.aspx"))
        {
            HttpContext.Current.RewritePath("~/Example.aspx");
        }
        else if (fullOrigionalpath.Contains("/changedbyurl/"))
        {
            //This is to correctly load the CSS & Java scrit files or Images etc on page.
            if (fullOrigionalpath.Contains("/changedbyurl"))
            {
                int start = 0;
                int end = 0;
                start = fullOrigionalpath.IndexOf("/changedbyurl");
                end = start + "/changedbyurl".Length;
                string strSubString = fullOrigionalpath.Substring(0, start);
                strSubString += "~" + fullOrigionalpath.Substring(end);
                fullOrigionalpath = strSubString;
            }
 
            string[] arr = fullOrigionalpath.Split('~');
            if (arr.Length > 1)
            {
                HttpContext.Current.RewritePath("~/" + arr[arr.Length - 1]);
            }
        }
 
    }
}
 

Web.config:













<system.webServer>
        <validation validateIntegratedModeConfiguration="false"/>
        <modules>
      <add name="rewriteurl" type="RewriteURL"/>
            <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </modules>
        <handlers>
            <remove name="WebServiceHandlerFactory-Integrated"/>
            <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </handlers>
    </system.webServer>
 

This working absolutely perfectly on local host but on production (having IIS6) CSS & JavaScript files are not loading.
 
On Master page inside head tag:
<link rel="stylesheet" href="jquery.megamenu.css" type="text/css" media="screen" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link href="popup-div.css" rel="stylesheet" type="text/css" />
    <link href="style1.css" rel="stylesheet" type="text/css" />
 
The above CSS & Javascript files are loading correctly on Local host but not on production server. I do not want to manually fix the paths for them as absolute path. I wonder why this is happening?
 

Added Later----------------------
To solve the CSS & Javascript files issue following approach is used by me:
protected void Page_Init(object sender, EventArgs e)
    {
        if (ViewState["OnceCalled"] != null)
            return;
        ViewState["OnceCalled"] = "Yes";
        objUtility.SetCssVersion(Page);
    }
    private void SetCssVersion(Control objHead)
    {
        foreach (Control c in EnumerateControlsRecursive(objHead))
        {
            if (c is HtmlLink)
            {
                HtmlLink lnkStylesheet = (HtmlLink)c;
                if (lnkStylesheet != null)
                {
                    if (lnkStylesheet.Href.Contains("?"))
                        lnkStylesheet.Href += "&" + Version();
                    else
                        lnkStylesheet.Href += "?" + Version();
 
                    if (!lnkStylesheet.Href.ToLower().Contains(".com") || !lnkStylesheet.Href.ToLower().Contains("http:"))
                    {
                        lnkStylesheet.Href = "Website name here/" + lnkStylesheet.Href;
                    }
                }
            }
        }
    }
But still Images are not loading.
I don't think this approach is good to make all image's URL absolute on page.
 
Or there's any other approach to handle this issue??
Added Later Ends----------------------
 
Any ideas??
 
Thanks
Ashish
Posted 9-Aug-12 19:43pm
tusDev1332
Edited 9-Aug-12 20:25pm
v3
Comments
Member 9618485 at 15-Dec-12 8:12am
   
I have the same problem. Have you got any solution for this?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

but on production (having IIS6) CSS & JavaScript files are not loading
Sounds like it is relative path issue. At times, after deployment the path referenced as a source for images, stylesheets, etc is not correctly formed. This leads to no file found at the location referred because of which images don't download or style-sheets does not get applied.
You would need to make sure that the path is correct such that file is found and used.
 
I would suggest you to use this Tip and resolve the path correctly before setting the source of the file: Resolving Paths in a Multi-Folder WebSite[^]
  Permalink  
Comments
tusDev1 at 10-Aug-12 1:22am
   
You are right, I am having the same problem. I have worked on displaying the CSS & Javascript files but to load images (that include many dynamic and in huge number some times) this approach dosen't fit as good for performance.
 
Please check the Later part of the question(added later).
 
Thanks
Ashish
Espen Harlinn at 19-Aug-12 9:23am
   
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 295
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100