Catch previous url in custom error page

J

Jason Zhou

Hi,

I have a question on how to get the previous url in custom error file. Here
is what I am doing:

If someone visit my website with a wrong file name or wrong directory name,
IIS will automatically show error 404 page, this was setup by default in IIS
web site properties/Custom Errors. I replaced that 404b.htm with my aspx
file try to handle this error by myself. the problem is that I can not get
previous url that user requested in my aspx page, I was thinking that
urlreferer property, but it does not work either. Does anybody has
experience on this issue before?

Any suggestion will be appreciated.

Jason
 
J

Jason Zhou

If I don't use that custom error page, do I have anyway to get control even
the file or directory specified in the request does not exist on my website?

Jason
 
Joined
Oct 6, 2006
Messages
1
Reaction score
0
Hi,
We can achieve this by doing following steps.

Create error.aspx file on wwwroot directory with following content

protected void Page_Load(object sender, EventArgs e)
{
string queryString = "";
if (Request.Url != null)
{
foreach (string s in Request.Url.ToString().Split(';'))
queryString = s;
}
Response.Redirect("http://yoursite.com/generalerror.aspx?aspxerrorpath="+queryString);
}

Open property window of "Default Web Site" under "Web Sites" folder in IIS.
Go to Custom Errors tab and edit HTTP403;4 and 404 error select Message type as File and browse above error.aspx file into this.

Now open your custom error file under your virtual directory namely "generalerror.aspx". Add following code in generalerror.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["aspxerrorpath"] != null)
{
string requestPageName = Convert.ToString(Request.Params["aspxerrorpath"]).ToLower();

if (!requestPageName.Contains("http://"))
requestPageName = "http://yourSite.com" + requestPageName;
lblRequestPage.Text = requestPageName;
}
}

And add one label into generalerror.aspx
<asp:Label ID="lblRequestPage" runat="server"></asp:Label>

Add following code in web.config file
<customErrors mode="RemoteOnly" defaultRedirect="generalerror.aspx">
<error statusCode="403" redirect="generalerror.aspx"/>
<error statusCode="404" redirect="generalerror.aspx"/>
</customErrors>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top