I am working on URL rewrite and I found one tutorial on asp.net site, The way I am doing it is
URL I am entering http://localhost:1573/WebNew/web/first-web
Now I have wriiten one class
public class FixURLs : IHttpModule
{
public FixURLs()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpModule Members
public void Dispose()
{
// do nothing
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
#endregion
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
/*// checking page extension
switch (System.IO.Path.GetExtension(app.Request.Url.AbsoluteUri.ToLower()))
{
case ".bmp":
case ".gif":
case ".jpg":
case ".jpe":
case ".jpeg":
case ".png":
case ".css":
case ".js":
case ".txt":
case ".swf":
// don't redirect, these requests may required in many cases
return;
break;
}*/
if (app.Request.RawUrl.ToLower().Contains("/web/"))
{
**if (app.Request.RawUrl.ToLower().Contains(".png")
|| app.Request.RawUrl.ToLower().Contains(".gif")
|| app.Request.RawUrl.ToLower().Contains(".js"))
{
return;
} **
DatabaseLayer dbLayer = new DatabaseLayer();
string urlFromBrowser = app.Request.RawUrl.ToLower();
string[] urlFormat = urlFromBrowser.Split('/');
urlFromBrowser = urlFormat.GetValue(2).ToString() + "/" + urlFormat.GetValue(3).ToString();
int WebId = dbLayer.GetWebURLId(urlFromBrowser.Trim());
app.Context.RewritePath("Default.aspx", "", "WebId="+WebId);
}
}
But the problem is that, it is not redirecting to the Default.aspx page.
I am getting the below error:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /WebNew/web/Default.aspx
I can see that it is requesting the URL /webNew/web/Default.aspx but I just need /webnew/default.aspx?WebId=2
Please assist me
EDIT
Thnks for the answer, but I am not able to accept your answer nor the comment button is working for me, that is the reason I am editing my post . I am getting JavaScript error: Object expected and Object question is null.
@Waqas Raja Thanks for you answer, it is working fine now. But when I placed the breakpoint at (!IspostBack) event of Default.aspx page, I can see that it is coming aroung 8 times. After loading the page few images are gone. Is multiple refresh is the issue? Any idea why it is going again and again to if(!IsPostBack) event. Thanks for your help
EDIT1
@Waqas Raja I will accept you answer after login from the differnt browser.
EDIT2
@Waqas Raja I am showing image on my home page like this
There are other few images which are also being displayed in this manner, they all are coming properly when loading normally but when I am using URL rewriting I can see only alt text.
I have tried this also
but same result. :-( any suggestion?
By checking the raw url i can see that there are many request other than the page, i.e. jpg,.js just like you mentioned. I have updated the code, is it correct? because I am not able to see images and few javascript functions.
EDIT3
@Waqas Raja Thanks a lot for your assistance. I can now see the images. I have removed the HTML image to asp:Image controla and keeping ImageURL as ~/images/facebook_icon.png.
But I still can't see the JS file effects on the page, I put src="~/JS/jQuery.js" but it is still not working. I have placed the code mentioned by you in my Class [Just where you have guided me].