Enable Yellow Screen of Death on MVC 3 - asp.net-mvc-3

I had to take over an MVC 3 project from another developer. One of the first things he did was to stop the yellow screen of death so that all exceptions are only logged to a file. You now only get a generic message saying an error has occurred.
I would like to switch it back on (since it gets really annoying having to check through the logfiles the whole time) - how do I do this.
I checked through the web.config but I can't see where this happens.
I did try doing customerrors=off, but that didn't do anything. Also removed the global error handling attribute, didn't do anything.
On further clarification, it seems if an exception occurs in a controller I do get the yellow screen of death, but if it occurs in a (razor) view I just get a standard generic error.
You can see the web.config here
You can see the global.asax here

This question is a little old, but maybe this will help someone. In addition to setting <customerErrors mode="Off" />, also set this under <system.webServer>: <httpErrors errorMode="Detailed" />
<system.webServer>
<httpErrors errorMode="Detailed"/>
</system.webServer>

Normally you set this in web.config in the customErrors element under system.web.
Just try to set mode=Off:
<customErrors mode="Off" />

In Global.asax you can remove filters.Add(new HandleErrorAttribute()); from public static void RegisterGlobalFilters(GlobalFilterCollection filters).
As pointed out in the comments - the problem was with a Custom Base controller overriding the OnException Method.

None of this worked for me. Check if someone might have added code to clear the error in the application error event handler.
protected void Application_Error(object sender, EventArgs e)
{
Exception lastException = Server.GetLastError().GetBaseException();
Log.Error("Global.asax: WebApplication error", lastException);
//****Server.ClearError();
}

Related

Xamarin Forms - InitializeComponent exception

I'm getting an exception on a Xamarin Forms content page with Xaml, on the initializecomponent method. The exception occurs on the generated xaml.gs page. I haven't changed any code form a working version. Maybe something upstream has changed. The exception message is ""
Value cannot be null.
Parameter name: binding
stack trace:
at Xamarin.Forms.BindableObject.SetBinding (Xamarin.Forms.BindableProperty targetProperty, Xamarin.Forms.BindingBase binding, System.Boolean fromStyle) [0x00011] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:293
at Xamarin.Forms.BindableObject.SetBinding (Xamarin.Forms.BindableProperty targetProperty, Xamarin.Forms.BindingBase binding) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:117
at ....LoginPage.InitializeComponent () [0x00045] in ...\obj\Debug\netstandard2.0\Views\Navigation\Login.xaml.g.cs:34
at FieldServices.LoginPage..ctor () [0x0002b] in ..\Views\Navigation\Login.xaml.cs:25
where occurrs:
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent() {
global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(LoginPage));
email = global::Xamarin.Forms.NameScopeExtensions.FindByName(this, "email");
password = global::Xamarin.Forms.NameScopeExtensions.FindByName(this, "password");
loginButton = global::Xamarin.Forms.NameScopeExtensions.FindByName(this, "loginButton");
}
I rarely get this error when pulling code down. First, double-check that your code is fine. In this case, I would make sure all the bindings are named correctly with no typos.
In the event that you cannot find an error and are thoroughly convinced there is nothing wrong with the code, try steps like
Clean and Rebuild
Close your Visual Studio and restart it
Hopefully the error is solved as easy as this.
On top of cleaning and rebuilding, manually delete all the obj and bin folders from your solution directory. Those xaml.gs files are generated files and sometimes they seem to not regenerate when they should.
I'm getting an exception on a Xamarin Forms content page with Xaml, on the initializecomponent method. The exception occurs on the generated xaml.gs page. I haven't changed any code form a working version. Maybe something upstream has changed.
It is not reproduceable by myside, but per experience there are a few things you can try:
Put your project in a short path like "C:\".
Try a new blank project, see if the error occors.
If none of the above works, you can try reinstalling Xamarin in your VS.
This was solved due to my not linking a static resource that was declared in the xaml

Elmah works on localhost, but not on production?

In my ASP.NET MVC 3 app, I've configured Elmah, and then Elmah.MVC for error logging. Both of which log just fine when running on localhost (Windows 7, IIS 6.1). On a production server (2008 R2, IIS 6.1), no errors are logged. I can browse to the /elmah directory in the site without problem (I've allowed remote access for now.) I've set the proper permissions to a folder for XML logging but nothing logged. I back-tracked to use the "in memory" logger, still no log. I've made sure modules and handlers were referenced correctly in both system.web and system.webserver.
I've browsed a lot of posts related to Elmah config issues, permissions, etc., but have not yet found the cause of this.
Are there other security/permissions issues that I'm missing on the production server related to Elmah? What else could be causing this?
Make sure you do NOT have "Read Only" checked on the folder. thanks
Not sure if this is still current but I was having a similar issue: it all worked perfectly on my local computer and on the web server when you had customErrors mode="Off" but if you changed customErrors to RemoteOnly then it would only work if you accessed the website locally.
The solution was to add a new Elmah filter on the FilterConfig section that guarantees elmah will always log the errors regardless of the customErrors mode. Here is the post with details: http://forums.asp.net/t/1687875.aspx?Elmah+Error+Log+is+not+working+my+production+Server
Here is the code snippet (credit to the person on the post):
public class FilterConfig {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
}
}
public class ElmahHandledErrorLoggerFilter : IExceptionFilter {
public void OnException(ExceptionContext context) {
//if (context.ExceptionHandled) // Log only handled exceptions, because all other will be caught by ELMAH anyway.
//We want elmah to log ALL exceptions
ErrorSignal.FromCurrentContext().Raise(context.Exception);
}
}

MVVM-light There is already a factory registered for INavigationService

I'm trying to adjust my WP8 project from self made MVVM implementation to MVVM Light.
The application compiles without errors, but when I open my MainPage.xaml in Expression Blend, I will get this error:
Class project.Services.INavigationService is already registered. App.xaml
My ViewModelLocator.cs:
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (ViewModelBase.IsInDesignModeStatic)
{
}
else
{
if (!SimpleIoc.Default.IsRegistered<INavigationService>())
{
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());
}
}
SimpleIoc.Default.Register<MainPage>();
SimpleIoc.Default.Register<SettingsEditViewModel>();
}
As you can see from my code comment, I've already tried the fix supposed here, but I'm still getting this error in Blend. There is no other place left where I could register the INavigationService, so what could be the problem?
Any ideas? :)
I had the same issue, and this seems to be a Visual Studio issue in combination with XAML-Designer, Static Factories/Locators and Design-Time creation of objects. However: The solutions were the following:
Register without a factory (not recommended)
SimpleIoc.Default.Register<INavigationService>();
Or if you want to use a factory, unregister before registering the factory
SimpleIoc.Default.Unregister<INavigationService>();
SimpleIoc.Default.Register<INavigationService>(() => new NavigationService());
Prevent the ViewModelLocator from being created more than once by the designer/Blend by making the constructor static
static ViewModelLocator() { ... }
The error is cumbersome but could happen in this scenario: You create objects during design-time (the ViewModelLocator within App.xaml probably) and whenever you change something in your Code, the Compiler is triggered and the ViewModelLocator gets re-created without ever unregistering the services. Therefore it will complain that in the factory has already been registered. In theory, when registering classes without factories multiple times, there should be an error as-well.
Might already be solved by now, but I think you can just solve the issue in your example case by not using the factory method override.
SimpleIoc.Default.Register<INavigationService, NavigationService>();
If you do need a factory method, then you can mix this line in with your factory method approach using the design mode check like you have been.
if (ViewModelBase.IsInDesignModeStatic)
{
SimpleIoc.Default.Register<INavigationService, NavigationService>();
}
else
{
SimpleIoc.Default.Register<INavigationService>(CreateNavigationService);
}
This seems like a super old question, but after hours of googling, this is the only question that's similar to my problem, so answering here for other people.
Make sure that you have IsDataSource on your ViewModelLocator:
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />
That solved my problem and my design data popped up straightaway.
Weirdly, after putting that on, all my "INavigationService is already registered" errors goes away!
Not sure why, but I only get this error when my xaml designer window is open. When I compile with it closed, the error goes away and the project is built and runs successfully.

IIS 7 Custom Error Page without Web.config

Is there any way to set a custom error page in IIS 7 without creating a web.config?
Unfortunately researching this particular topic has been very difficult because there are SO many articles on how to do it with a web.config. What I'm looking for is either buried beneath the 8 million results I don't want or it's not possible.
Yes, there is. It involves either subscribing to the Application_Error event in Global.asax or by writing a custom ErrorHandlerAttribute.
Darin already gave the correct answer, but I want to go into a little more depth.
In any ASP.NET application, given it is Web Forms, MVC, or raw ASP.NET, you can always use Application_Error Global.asax. If your ASP.NET application does not have a Global.asax, all you need to do is right-click your project in Solution Explorer, Add New Item, and choose Global Application Class. You should only have this option available if you don't already have one.
In your Global.asax, if you don't already see it, you can add Application_Error as shown below:
protected void Application_Error(object sender, EventArgs e) {
}
This will be called automatically by ASP.NET whenever there is an error. But as stated here, this is not perfect. Specifically:
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.
In Application_Error you can process the uncaught exception with Server.GetLastError(). This will provide you the Exception that was thrown, or null. I am not sure why this handler would be called if an exception didn't occur, but I believe that it is possible.
To redirect the user, use Response.Redirect(). Whatever you pass for the url is going to be sent directly to the browser without any further processing, so you can't use application-relative paths. To do that I would use this method in combination with VirtualPathUtility.ToAbsolute(). For example:
Response.Redirect( VirtualPathUtility.ToAbsolute( "~/Error.aspx" ) );
This redirect will be a 302 (temporary redirect) rather than a 301 (permanent), which is what you want in the case of handling errors. It's worth noting that this overload of Response.Redirect is the same as calling the overload Response.Redirect(url, endResponse: true). This method works by throwing an exception, which is not ideal in terms of performance. Instead, call Response.Redirect(url, false) immediately followed by Response.Complete​Request().
If you're using ASP.NET MVC, [HandleError] is also an option. Place this attribute on your Controller or on an Action within a controller. When this attribute is present, MVC will display the Error view, found in the ~/Views/Shared folder.
But you can make this even easier for yourself. You can automatically add this attribute to call Controllers in your project by creating a FilterConfig class in your project. Example:
public class FilterConfig {
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new HandleErrorAttribute());
}
}
And then add FilterConfig.RegisterGlobalFilters( GlobalFilters.Filters ); to your Application_Start() in Global.asax.
You can read more about the HandleErrorAttribute at https://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute(v=vs.118).aspx.
But as stated above, both of these methods will never cover absolutely all errors that can occur during the processing of your application. It's not possible to provide the best user experience for all possible errors without using Web.config or configuring IIS manually.

Razor view errors not displaying source code

Whenever an error is thrown in my Razor view (.cshtml), I get a yellow screen that states
The source code that generated this
unhandled exception can only be shown
when compiled in debug mode. To enable
this, please follow one of the below
steps, then request the URL:
It then says to either set the <# Page Debug="true"> in the view or set the <compilation debug="true"> in the web.config.
I checked my web.config and the <compilation debug="true"> is already set. To exhaust other options, I tried adding it to all the web.config files in the view folders, but no change.
I also checked that the projects are being compiled as Debug and not Release. Any thoughts as to why the source code where the error is being thrown is not being displayed?
Edit: My application was updated from ASP.NET MVC 2 to MVC 3.
Apparently source errors are not displayed when the trust level is set to medium.
I forgot to remove <trust level="medium" /> from the web.config file after I was done testing in medium trust.
1)
source errors does not appear if you do not insert
customErrors mode="Off"
and
compilation debug="true"
in your web.config
2)
when you're in medium trust you can use mvc3 provided you :
cast your return view(xxx) to return view((Object)xxx)
or, in vb: return view(xxx) to return DirectCast(view(xxx), Object)
and
Return View(model) to Return View((Object)model)
or to Return View(DirectCast(model, Object))
and
you do not use #ViewBag

Resources