Click here to Skip to main content
15,888,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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:
C#
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:
C#
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.web>

<httphandlers>
<remove verb="*" path="*.asmx">
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false">

<httpmodules>
<add name="rewriteurl" type="RewriteURL">
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">


XML
<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:
XML
<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:
C#
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
Updated 9-Aug-12 19:25pm
v3
Comments
[no name] 15-Dec-12 8:12am    
I have the same problem. Have you got any solution for this?

1 solution

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[^]
 
Share this answer
 
Comments
tusDev1 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 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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900