Reputation: 497
When I am running one of my pages and someone changes the url to a non existing one, I want them to redirect to my login page. On the internet I found the following code, which I put in my Web.config file:
<customErrors mode="On"
defaultRedirect="~/ErrorPages/Login.aspx" />
I put this inside this:
<system.web>
</sytem.web>
However, this does not work for me, I still get to see this:
I tried to change the mode from On to Remote Only and vice versa, but that did not help. I also tried to change the defaultRedirect from ~/ErrorPages/Login.aspx to ~/Login.aspx but that also did not work. Can anyone help me out here? Thanks in advance.
EDIT:
The Login.aspx page is located inside a subfolder called Pages. Unfortunately this: ~/Pages/Login.aspx doesn't work either
Upvotes: 0
Views: 105
Reputation: 4069
<configuration>
<system.web>
<compilation targetFramework="4.0" />
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="login.aspx" />
</customErrors>
</system.web>
Update :
An error handler that is defined in the Global.asax
file will only catch errors that occur during processing of requests by the ASP.NET
runtime. For example, it will catch the error if a user requests an .aspx file that does not occur in your application. However, it does not catch the error if a user requests a nonexistent .htm file
. For non-ASP.NET errors, you can create a custom handler
in Internet Information Services (IIS). The custom handler will also not be called for server-level errors.
--MSDN
You can also handle this error in your global.asax Application_Error event
.
protected void Application_Error(Object sender, EventArgs e)
{
HttpException serverError = Server.GetLastError() as HttpException;
if (null != serverError)
{
int errorCode = serverError.GetHttpCode();
if (404 == errorCode)
{
Server.ClearError();
Server.Transfer("Your virtual path");
}
}
}
Upvotes: 2
Reputation: 4219
Not sure about your error, however it should look like this:
<system.web>
<customErrors mode="On" defaultRedirect="~/ErrorPages/YourErrorPage.aspx">
<error statusCode="404" redirect="~/ErrorPages/YourErrorPage.aspx" />
<error statusCode="500" redirect="~/ErrorPages/YourErrorPage.aspx" />
</customErrors>
..........
.........
</sytem.web>
I would not point to the login page in case of errors because if you have an error on such page you will never come right. Rather place a linkLabel on the error page to get redirected to the login page.
Upvotes: 0