I'm converting an application which uses MVC1 , Visul Studio 2008 and .Net 3.5 to an MVC3, .Net4 vs2010 application.
I got it basically working but I have to implement a few thing out to get it going along.
To force a logout when the user expired I used to have this code in Global.asax.cs
void Session_End(object sender, EventArgs e)
{
//Todo make this work with mvc3/.net4
//IFormsAuthentication FormsAuth = ObjectFactory.GetInstance<IFormsAuthentication>();
//FormsAuth.SignOut();
//Server.Transfer("~/AccessControl/AdLogin.aspx");
}
But it crashed after converting to .Net4/MVC3, looked like something it depended upon was not initialized yet.
I assume that I need to move it to a new event, any idea which? If not that how do I do this in the new environment.
It is an ugly solution but I wound up wrapping it in a structure map exception Try/Catch and swallowing the exception. I'd really like to find a better solution if anyone has one.
Cal-
Related
I'm coming at the end of the development of my application.
Everything is working fine, but if once somebody got an unexpected exception, I would like to be able to:
Redirect him to a user-friendly page explaining that we got a problem.(Specifying a controller/action)
Log every information of the stack trace, current controller/action, parameter, session data, ...
What is the best way to do it with asp.net MVC?
EDIT
In complement of the great answer:
I integrated elmah like described here: http://joel.net/logging-errors-with-elmah-in-asp.net-mvc-3--part-1--setup
And then I specified some custom error page. So now I've all I need!
I would recommend you use "Elmah" http://code.google.com/p/elmah/
This package is a godsend - does exactly what is shown on the box, with close to ZERO changes to your source code.
It is also available via NuGet at http://nuget.org/packages/elmah
Use action filters.
http://blogs.msdn.com/b/gduthie/archive/2011/03/17/get-to-know-action-filters-in-asp-net-mvc-3-using-handleerror.aspx
You can catch errors in the global.asax. There is a method protected void Application_Error(object sender, EventArgs e) where you can do what ever you want.
Take a look at PostSharp, neat and easy to use. Starter version available to use after free registration. NuGet package also available http://nuget.org/packages/PostSharp
i'm using n2cms + asp.net mvc,
when the site is uploaded to a webserver, and an exception is thrown in the aspx page, the page appear blank, and there is nothing in the page, even if i turned off the CustomErrors in web.config
but when the site is running on my computer visual studio simply show me the exception,
is there a way to catch the exception in this situation?
This may be due to your hosting configuration. You could take a look at ELMAH for an easy way of logging exceptions.
You can handle all global errors in your Global.asax's method called Application_Error - http://msdn.microsoft.com/en-us/library/24395wz3.aspx . It will work for simple cases. But I strongly recommend to use ELMAH
N2CMS makes calls to SwallowExceptions(). This is why you are receiving a blank page instead of an exception. You could look for that method call and comment it.
The standard "Potentially dangerous request" is expected, resolve it with [ValidateInput(false)] or something more granular if you wish.
In my case, it happens when I make a call to FbApp.Session. I have a BaseController that all Controllers derive from. In there is an override OnActionExecuting call. Pretty common scenario.
Inside of OnActionExecuting, I use the Facebook C# SDK to manage my use membership. Using if (FbApp.Session != null) is how I check if the user is authenticated.
While running the debugger, it points to the call to FbApp.Session as the source of the exception stating "A potentially dangerous request...", due to HTML in one of the posted request vars - even though the actual action I'm posting to has [ValidateInput(false)] attribute.
What is going on inside the Facebook C# SDK that would cause this behavior? How can this be resolved?
Edit: looks like this could be a bug in ASP.NET MVC 3 RC2 (I haven't upgraded to full release yet). I'll upgrade and report my findings.
The answer is, make sure you're on ASP.NET MVC3 RTM, and have <httpRuntime requestValidationMode="2.0" /> in web.config.
I am trying to Catch exceptions and log it.
Presently I have written a Utility method and Passing the Exception to it in the catch block and finally logging the application using MS Enterprise Library 4.0.
I want to know is there any other better way to log errors.
Show exception message in the same view for some type of exceptions for rest navigate to error page.
If someone can help me on this!
Navish
You could try elmah for error logging if that is what you are looking for.
If you were rolling your own Exception logging you could create a base Controller class that overrides the OnException method
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
ExceptionPolicy.HandleException(filterContext.Exception, "MyPolicy");
base.OnException(filterContext);
}
}
Elmah
Elmah could be great option for you. It is easy to use, after installing nuget package is ready for use.
Custom logging with log4net etc.
If you want to log errors by your own, I recommend this example app: https://github.com/mholec/mvcexceptionhandler where is explained global logging with your favorite log tool (log4net, nlog, etc.)
Application Insights
Another and maybe the best way is to use Application Insights. Application Insights you can install quickly using NuGet and you will get great overview of your application errors, website availability and you can get more information about your users. This service is free.
I recommend ELMAH http://code.google.com/p/elmah/
You can get started very quickly with it by using Nuget to set it up.
http://nuget.codeplex.com/
Setup Nuget.
In vstudio ->
View -> Other Windows -> Package
Manager
List-Package -filter elmah
install-package elmah and
you're all set.
You can view the log just like trace.axd by going to http://mysite/elmah.axd
and manually throw errors to elmah like this:
try {
}
catch (Exception ex)
{
ErrorSignal.FromCurrentContext().Raise(ex);
}
hope that helps,
-fs
I have implemented my own VirtualPathProvider for loading 'embedded' views.
This works very well when running from Visual Studio, but I get the 'The view not found' message when running on IIS6.
Is there anything missing in web.config, or could there be any other problem?
I have added some logging and it seems that even though I register the Custom VirtualPathProvider in Application_Start, the System.Web.Hosting.MapPathBasedVirtualPathProvider is still used.
For the combination Custom VPP + IIS6 + Precompiled site, we need to add the VPP from AppInitailize();
public static class AppStart
{
public static void
{
// code to be executed automatically by the framework
}
}
See also:
http://sunali.com/2008/01/09/virtualpathprovider-in-precompiled-web-sites/
Is yours not used at all? VirtualPathProviders operate in a stack so, in your VirtualPathProvider, you should notice that the base class member "Previous" is actually the instance of "MapPathBasedVirtualPathProvider".
If you:
Attach your debugger to IIS
Make a change in your web.config, then change it back, then save (to trigger an recycle) - while still attached!
Place a breakpoint in your VPP in FileExists
Hit a page
Does your VPP get hit? If so, it may be an issue that after the first request, MVCs ViewEngine caching is preventing your VPP from getting hit for additional requests...