IIS7 404 errors
I have an ASP.NET 3.5 site running in IIS 7
I am trying to have 404 throw a 404 status code first time round.Currently if you type in
http://www.madeupsiteforexample.com/somethingmadeup
You receive a 302 follow by a 200.
I am trying to get this to throw a 404 code first time around and display the 404 page i have setup (/FileNotFound.aspx)The problem i'm having is either 404 page is displayed with a 200 code, or IIS takes over when it sees the 404 status code and displays its own horrible 404 page and not my custom created one.
I have tried using modules, global.asax and setting the status code on the code behind of my 404 page. All result in IIS taking over.
Here is my Global.asax implementation
protected void App开发者_Go百科lication_Error(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = 404;
}
Now i did solve this problem by Server.Transferring my request.
This however causes all session to be null and any code referring to Session causes an exception. Also code trying to retrieve items from resource files cause exceptions.Any suggestions or articles on the "right way" to do 404's in IIS7?
i also got stuck on this for iis 7 .. you could try this one as it worked for me...
<httpErrors existingResponse="PassThrough" />
This person also had the same issue as you. IIS7 Overrides customErrors when setting Response.StatusCode?
here is an expiation of what is happening http://www.fidelitydesign.net/?p=21
Have a look at this tutorial, it's normal to set up the custom error page(s) via configuration, not in code.
You can change which page is shown at 404 by modifying the "error pages" setup under "Default Web Site" (or specific to your application)
Use Server.TransferRequest (not supported in IIS6) instead of Server.Transfer if you want Session to still be available.
But my preferred approach is to call Response.StatusCode = 404;
in your FileNotFound.aspx
and set web.config to:
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1"/>
<error statusCode="404" prefixLanguageFilePath="" path="/FileNotFound.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
For others if you're like me and simply display the home page with a message at the top for 404 errors, you can test if it's a 404 request since internally the new request has the status code is appended to the url i.e.
if (Request.Url.ToString().Contains("?404;"))
{
Response.StatusCode = 404;
Util.DisplayMessage("The page you are looking for no longer exists. If you navigated to this page by clicking a link within this site please <a href='" + ResolveUrl("~/contact.aspx") + "'>contact us</a> to let us know.");
}
Also paths for images, scripts etc can be wrong if they're relative to FileNotFound.aspx
(i.e. not to site root '/path' or resolved at server '~/path') and the missing page (i.e. current url) is in a different folder. To fix I tell the browser to base relative paths to where FileNotFound.aspx
is located with the base tag and tell the server to match with Context.RewritePath i.e.
Page.Header.Controls.AddAt(0, new LiteralControl("<base href='" + Request.Url.Scheme + "://" + Request.Url.Authority + VirtualPathUtility.ToAbsolute("~/") + "'/>"));
Context.RewritePath("~/");
精彩评论