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)
{
string strLastURL = "";
string fullOrigionalpath = HttpContext.Current.Request.Url.ToString().ToLower();
if (fullOrigionalpath.Contains(".aspx") && !fullOrigionalpath.Contains("404error.aspx"))
{
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;
public class RewriteURL : IHttpModule
{
public RewriteURL()
{
}
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/"))
{
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">
<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