Created
November 15, 2011 15:02
-
-
Save JoshClose/1367272 to your computer and use it in GitHub Desktop.
ASP.NET MVC, Ninject.Web.Mvc and 404’s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void Application_Error( object sender, EventArgs e ) | |
{ | |
var exception = Server.GetLastError(); | |
Response.Clear(); | |
var httpException = exception as HttpException; | |
var routeData = new RouteData(); | |
routeData.Values.Add( "controller", "Error" ); | |
if( httpException == null ) | |
{ | |
routeData.Values.Add( "action", "Index" ); | |
} | |
else //It's an Http Exception, Let's handle it. | |
{ | |
switch( httpException.GetHttpCode() ) | |
{ | |
case 404: | |
// Page not found. | |
routeData.Values.Add( "action", "HttpError404" ); | |
break; | |
case 500: | |
// Server error. | |
routeData.Values.Add( "action", "HttpError500" ); | |
break; | |
// Here you can handle Views to other error codes. | |
// I choose a General error template | |
default: | |
routeData.Values.Add( "action", "General" ); | |
break; | |
} | |
} | |
// Pass exception details to the target error View. | |
routeData.Values.Add( "error", exception ); | |
// Clear the error on server. | |
Server.ClearError(); | |
// Call target Controller and pass the routeData. | |
IController errorController = new ErrorController(); | |
errorController.Execute( new RequestContext( new HttpContextWrapper( Context ), routeData ) ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace Ninject.Web.Mvc | |
{ | |
/// <summary> | |
/// A controller factory that creates <see cref="IController"/>s via Ninject. | |
/// </summary> | |
public class NinjectControllerFactory : DefaultControllerFactory | |
{ | |
/// <summary> | |
/// Gets the kernel that will be used to create controllers. | |
/// </summary> | |
public IKernel Kernel { get; private set; } | |
/// <summary> | |
/// Initializes a new instance of the <see cref="NinjectControllerFactory"/> class. | |
/// </summary> | |
/// <param name="kernel">The kernel that should be used to create controllers.</param> | |
public NinjectControllerFactory(IKernel kernel) | |
{ | |
Kernel = kernel; | |
} | |
/// <summary> | |
/// Creates the controller with the specified name. | |
/// </summary> | |
/// <param name="requestContext">The request context.</param> | |
/// <param name="controllerName">Name of the controller.</param> | |
/// <returns>The created controller.</returns> | |
public override IController CreateController(RequestContext requestContext, string controllerName) | |
{ | |
var controller = Kernel.TryGet<IController>(controllerName.ToLowerInvariant()); | |
if(controller == null) | |
return base.CreateController(requestContext, controllerName); | |
var standardController = controller as Controller; | |
if(standardController != null) | |
standardController.ActionInvoker = new NinjectActionInvoker(Kernel); | |
return controller; | |
} | |
/// <summary> | |
/// Releases the specified controller. | |
/// </summary> | |
/// <param name="controller">The controller to release.</param> | |
public override void ReleaseController(IController controller) { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment