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].
The problem is with your rewrite path, replace it with the following
Edit 1 Your requirements have been changed, however let me try to explain whats going wrong.
The problem must be with your relative paths of images and also it could occur in case of external javascript and css.
You should use path relative to your root directory.
~
refers to Root directory of virtual directory whose resource (usually page or sometime external css) is being accessed.~/pathToImagesFolder/imageName.ext
You should be cautious when including javascript and css and use path relative to home directory of your website or virtual directory. However inside css there is no step needed to be taken in this regard.
Regarding
IsPostBack
problemI guess the issue is with your if condition
Here you are redirecting any request which contains
/web/
in URL and most probably inside your page you are accessing some resources like images or external css or js which contains/web/
in their url and so when the request reaches it is being redirected to default.aspx and the issue raises. You can confirm it by looking intoRequest.RawUrl
inPage_Load
event ofDefault.aspx
So, you need more checks when comparing requested resource path and redirecting. More beautiful it be if you check and skip redirecting all urls which ends with the extensions like
.css .jpg .JPG .js .gif
e.t.cI hope your understandings build now.
Edit 2 You need to skip all the other extensions