I am trying to implement custom error pages but think in the BoilerPlate Framework (MVC5/jQuery MPA) ASP.NET is handling static file requests.
For example, if I specify ASP.NET custom errors in web.config:
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="/Errors/404.aspx"/>
</customErrors>
and visit a controller that does not exist, my.website.com/nullcontroller/nullaction, I correctly get the ASP.NET 404 page 404.aspx.
Now, I can also configure IIS custom errors in web.config:
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="Errors\404.html" responseMode="File"/>
</httpErrors>
</system.webServer>
However now, if I visit a non-existing page like my.website.com/notexist.html I still get the ASP.NET 404.aspx error page, not the IIS 404.html error page. ASP.NET is trying to server the static file and can't find it so returns the ASP.NET 404 page. Normally IIS would handle the static file request and use the IIS 404 page.
I prove this to myself by modifying the routes in RouteConfig.cs and adding a route to explicitly ignore the notexist.html static file.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("notexist.html"); // My test URL
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
...
...
Now if I visit my.website.com/notexist I get the intended page, the IIS 404.html error page. ASP.NET did not attempt to handle the static file request. Instead IIS attempted to find the file and failed so served it's 404 page.
I am just wondering, how is BoilerPlate configured (e.g. where in the code/config) such that html files are handled by ASP.NET and not IIS. By default, in the sample Visual Studio MVC application, static files are handled by IIS.
I did find this interesting code in the ABP source in class Abp.Web.Mvc.Controllers.AbpController in the OnException method:
context.HttpContext.Response.TrySkipIisCustomErrors = true;
but changing this to false did not have any affect.
Thanks
11 Answer(s)
-
0
Go to NuGet Package Manager and Type StaticFiles in "Browse" Category. Then it will display many staticfiles details but we need to choose and Install "Microsoft.AspNetCore.StaticFiles".
We can add this following method in "Startup.Cs" and every extension method runs as a sequence.
UseStaticFiles() UseDefaultFiles() UseFileServer() UseStaticFiles()
We can call the UseStaticFiles extension method from Startup.Cs and it makes the files in web wwwroot or web root as servable.
Code
app.UseDefaultFiles(); // Call first before app.UseStaticFiles() app.UseStaticFiles(); // For the wwwroot folder
-
0
Hi alper,
thank you for your reply but I don't think your solution applies to me as I am not using ASP.NET Core.
-
0
Hi @devkev2403,
Do you want only IIS to handle custom errors ? If so, you can set customErrors to Off.
Thanks.
-
0
HI ismcagdas,
turning customErrors off seems like cheating.
I want ASP.NET to handle errors for requests that enter the and then fail in the ASP.NET pipeline.
I want IIS to handle errors for requeests that do not enter the ASP.NET pipeline e.g. static files that are missing so give a 404.
Why do requests for static files e.g. <a class="postlink" href="http://www.abpzerosite.com/image.jpg">http://www.abpzerosite.com/image.jpg</a> enter theASP.NET pipeline - I can't find any routing configuration for this in the source?
Turning customErrors off does not actually seem to work. I am investigating this now. Is that because of this code:
context.HttpContext.Response.TrySkipIisCustomErrors = true;
-
0
I have my preferred solution now.
I set customErrors off, and in httpErrors I was missing the existingResponse attribute. So now I use this:
<system.webServer> <system.webServer> <httpErrors errorMode="Custom" existingResponse="Replace" defaultResponseMode="File"> <remove statusCode="400"/> <remove statusCode="401"/> <remove statusCode="402"/> <remove statusCode="403"/> <remove statusCode="404"/> <remove statusCode="405"/> <remove statusCode="406"/> <error statusCode="400" path="Errors\400.html" /> <error statusCode="401" path="Errors\401.html" /> <error statusCode="402" path="Errors\402.html" /> <error statusCode="403" path="Errors\403.html" /> <error statusCode="404" path="Errors\404.html" /> <error statusCode="405" path="Errors\405.html" /> <error statusCode="406" path="Errors\406.html" /> </httpErrors> </system.webServer>
This works well, but I can not find where the text for other errors come from. For example, if I get a 501, I don't get the default IIS 501 page in full but I do get the appropriate 501 error message.
-
0
UPDATE: there is a problem with using httpErrors which is that it stops the error messages from UserFriendlyException being displayed. I am going to read up more on this but wanted to update this thread.
-
0
thanks for your feedback
-
0
No problem. I haven't really made any more progress :(
system.web/CustomErrors must be on for text in UserFriendlyExceptions to be displayed on the client.
Status code 401-Unauthorized should not have an explicit customError as this causes issues with redirection to the login page.
system.webServer/httpErrors can not be on or it also breaks UserFriendlyException. I haven't got to the bottom of why but assume it is in ABP framework code.
-
0
To add more detail to this, this is a problem!
customErrors must be on for UserFriendlyException. This is fine.
httpErrors must be off for UserFriendlyException.
But then, if the user attempts to visit a URL they don't have permission for, IIS displays its default 403 error page because you can't have httpErrors configured in web.config.
-
0
Hi @devkev2403,
Thanks for detailed explanation, can you create an issue here <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/issues">https://github.com/aspnetzero/aspnet-zero/issues</a> and we will investigate it next week.
Thanks.
-
0
Issue created -- customErrors and httpErrors Conflict #533