Catch previous url in custom error page

Discussion in 'ASP .Net' started by Jason Zhou, Jun 24, 2004.

  1. Jason Zhou

    Jason Zhou Guest


    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 Zhou, Jun 24, 2004
    1. Advertisements

  2. Jason Zhou

    Jason Zhou Guest

    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 Zhou, Jun 24, 2004
    1. Advertisements

  3. Jason Zhou


    Oct 6, 2006
    Likes Received:
    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;

    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 = "" + 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"/>
    csandbhor, Oct 6, 2006
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.