MVC Area Error Handling - asp.net-mvc

I have a MVC project set up with 3 areas. In the main project I have error handling set up
using custom errors in the web.config.
<customErrors mode="On" defaultRedirect="~/Error/HttpError">
<error statusCode="404" redirect="~/Error/Http404" /> </customErrors>
This cause the site to redirect to a error controller in the root and then show the error view.
This works OK in the root site, however when I throw an exception in the home controller of one
of the area sites it the message below.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.
Details: To enable the details of this specific error message to be viewable on the local server machine, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "RemoteOnly". To enable the details to be viewable on remote machines, please set "mode" to "Off".
Is it the case that error handling in the root site cannot be used in the area sites?
Thanks
John.

Does the area have it's own web.config that might be setting it's own error handling options? Can you put a breakpoint in Application_OnError to see if another error is occuring (eg. one loading the error page - eg. if you've turned off Buffering, it won't be able to do the redirect)?
You could also try adding the something like this to the main web.config to see if it it makes any difference:
<location path="nameOfArea">
<system.web>
<customErrors mode="On" defaultRedirect="~/Error/HttpError" />
</system.web>
</location>

Related

Why does IIS show it's own error message for 403 errors instead of relying on my customErrors element?

I'm getting a request like http://example.com/%2f..%2fwindows%2fsomething.ini and IIS is displaying "Forbidden URL - HTTP Error 403. The request URL is forbidden."
I think this request is getting blocked at the IIS level. How can I get IIS to let this error fall through to my application so I can show the error page the rest of my application uses? 404 errors fall through nicely as expected using the customErrors attribute in the web.config:
<customErrors mode="RemoteOnly" defaultRedirect="~/content/error.html">
</customErrors>
If I enter a simpler invalid URL like http://example.com/asdfasdfas/asdfasdf then my customErrors handler kicks in as desired.
403 is iis error. look like your asp.net error is not firing. you could try to add below code in the web.config file:
<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom">
<remove statusCode="403"/>
<error statusCode="403" path="/errorpage.aspx" responseMode="ExecuteURL"/>
you could also use iis manager to modify the changes:
1)open iis manager, select your site.
2)double click error pages.
3)set error page as suggested below or as per your requirement.
Why is IIS 7 showing me a default 403 page?
ASP MVC 5 - 403 customError not working

Custom error page only works for 404 errors

I have developed an MVC5 page and have this in web.config:
<customErrors mode="On" defaultRedirect="/Home/Error" />
the problem is that this only works when I enter a non existing file in URL, for example, "/images/nonexisting". However, when I enter just "/images/" in URL (this folder does exist), an
"Error HTTP 403.14 - Forbidden"
is shown instead of custom error page.
How can I solve this?
EDIT: this is not duplicated. Referenced page is about defaultRedirect not taking into account at all. In my case, it works but only for 404 errors. For 403.14 errors, it does not work.
One other fact took my attention.
When I placed mode=Off in customErrors tag, and tried to load "/images/nonexisting", I get this error:
However, when I tried to load /images/, I got the following page:
As you see, both pages have different look. I think that is why it does not work, but I am not sure how to solve it.
It seems 404 errors are trapped by .NET Framework but 403 errors are trapped by the Web server not actually reaching the web site (that may be why custom error page is not shown). The only solution to this should be to try to configure this directly in the web Server?
Furthermore, I tried adding this to web.config, but it did not work either:
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="403" redirect="~/Home/Error" />
</customErrors>

Forms Authentication not redirecting

I am trying to incorporate forms authentication into my web application. If I try to load any page without logging in it will not redirect the user to the login page but instead load the requested page. As an example if I try to load ~/Home/About without logging in it will proceed to load the page without redirecting me to ~/Account/Login. This is the first time I have tried to implement form authentication and I am struggling to see the problem.
I believe I have added the correct settings to the web.config file on the root. They are as follows.
<system.web>
<authentication mode="Forms">
<forms name="chasAuth" loginUrl="~/Account/Login"/>
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
I have another web.config file that resides in the Views folder. I have also tried to add these setting to this file however this results in a different problem. I get the following error message:
Description: An error occurred during the processing of a configuration file >required to service this request. Please review the specific error details >below and modify your configuration file appropriately.
Parser Error Message: It is an error to use a section registered as >allowDefinition='MachineToApplication' beyond application level. This error >can be caused by a virtual directory not being configured as an application in >IIS.
I have tried to configure the virtual directory as an application in IIS as my research into the error has suggested however I can't seem to find the application in IIS. I am using localhost, or the ASP.NET Development Server, to debug my code. It is not up on a server yet. Would this be the issue?

"Error Loading Page" message, show error

I'm developping my first MVC4 application and have now on several occasions gotten a 'Error loading page' message. This is nice if you're in a production environment but when developing an application it would be much more productive to see what's wrong, see the exception details. Is there some way or setting to get to the exception information wich causes this message ?
regards,
Jurjen.
It its a .Net exception locate the customErrorselement in your Web.config and set the attribute mode="Off" or mode="RemoteOnly".
If its a javascript error open your javascript console and watch for errors.
If its a .Net exception but the YSOD isn't rendered by the browser because it was requested via ajax, the fastet way to get to the output is to use a debugging proxy like fiddler and watch the html result there.
If its neither of the options, you have to provide more details.
Have you ever tried to run the application in the debugger?
You can enable debugging and set the error display mode in the web.config
<configuration>
<system.web>
<compilation debug="true" ...>
...
</compilation>
<customErrors mode="On|Off|RemoteOnly" ...>
(set this to Off or RemoteOnly) when debugging
...
</customErrors>
</system.web>
</configuration>
How to enable debugging for ASP.NET applications
How to set the customErrors Element
in _layout set
$.mobile.ajaxEnabled = false;
This solved my problem
Cheers

Setting CustomErrors to On in Web.Config crashes site

I have an ASP.NET MVC3 site where CustomErrors are set to Off in Web.config. Naturally, I want to set it to on. However, when setting it to on:
<customErrors mode="On" defaultRedirect="~/Error.aspx" />
accessing the site gives the error-page promptly.
Having it to Off doesn't give an error, and the site functions perfectly. Is there a silent error, that customErrors catches, but I don't see? Does it have something to do with me using session-variables?
This happens when deployed to IIS, but not in my dev-environment.

Resources