protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            GlobalConfiguration.Configure(WebApiConfig.Register);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            // Start - Exception Handling
            // to set different views for different errors
            HandleErrorAttribute a1 = new HandleErrorAttribute();

            a1.ExceptionType = typeof(DivideByZeroException);
            a1.View          = "DivideByZeroError";
            GlobalFilters.Filters.Add(a1);  // no need to apply [HandleError] on indidual actions or controllers

            // to set different views for different errors
            HandleErrorAttribute a2 = new HandleErrorAttribute();

            a2.ExceptionType = typeof(NullReferenceException);
            a2.View          = "NullRefError";
            GlobalFilters.Filters.Add(a2);  // no need to apply [HandleError] on indidual actions or controllers

            // if none of above errors matches
            GlobalFilters.Filters.Add(new HandleErrorAttribute());  // no need to apply [HandleError] on indidual actions or controllers

            // End - Exception Handling
        }
        public void MasterProperty() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "Master", String.Empty);
        }
        public void MasterProperty() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "Master", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            var errorAttribute = new HandleErrorAttribute {
                View = "NotFound"
            };

            filters.Add(errorAttribute);
        }
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            var errAttribute = new HandleErrorAttribute();

            errAttribute.View = "~/Views/Shared/Error.cshtml";

            filters.Add(errAttribute);
        }
示例#6
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            var errorFilter = new HandleErrorAttribute();

            errorFilter.View = "Error";

            filters.Add(errorFilter);
        }
        public void HandleErrorAttributeReturnsUniqueTypeIDs() {
            // Arrange
            HandleErrorAttribute attr1 = new HandleErrorAttribute();
            HandleErrorAttribute attr2 = new HandleErrorAttribute();

            // Assert
            Assert.AreNotEqual(attr1.TypeId, attr2.TypeId);
        }
        public void MasterProperty()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "Master", String.Empty, false /* testDefaultValue */, true /* allowNullAndEmpty */);
        }
示例#9
0
        public void ViewProperty()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "View", "Error", nullAndEmptyReturnValue: "Error");
        }
示例#10
0
        public void MasterProperty()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "Master", String.Empty);
        }
        public void ViewProperty()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "View", "Error", false /* testDefaultValue */, true /* allowNullAndEmpty */, "Error");
        }
示例#12
0
        public void ExceptionTypePropertyWithNullValueThrows()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { attr.ExceptionType = null; }, "value");
        }
示例#13
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            HandleErrorAttribute atr = new HandleErrorAttribute();

            atr.ExceptionType = typeof(Exception);
            atr.View          = "Error";
            filters.Add(atr);
        }
示例#14
0
        public void HandleErrorAttributeReturnsUniqueTypeIDs()
        {
            // Arrange
            HandleErrorAttribute attr1 = new HandleErrorAttribute();
            HandleErrorAttribute attr2 = new HandleErrorAttribute();

            // Assert
            Assert.NotEqual(attr1.TypeId, attr2.TypeId);
        }
示例#15
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            var errorAttribute = new HandleErrorAttribute();

            filters.Add(new AuthorizeAttribute());
            errorAttribute.View = "Home/Error";
            filters.Add(errorAttribute);
            filters.Add(new SalvisAntiForgeryToken());
        }
        public void ExceptionTypePropertyWithNullValueThrows()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { attr.ExceptionType = null; }, "value");
        }
示例#17
0
        public void OnExceptionWithNullFilterContextThrows()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            Assert.ThrowsArgumentNull(
                delegate { attr.OnException(null /* filterContext */); }, "filterContext");
        }
示例#18
0
        public void ExceptionTypePropertyWithNonExceptionTypeThrows()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            Assert.Throws <ArgumentException>(
                delegate { attr.ExceptionType = typeof(string); },
                "The type 'System.String' does not inherit from Exception.");
        }
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
            var err = new HandleErrorAttribute();

            filters.Add(new HandleErrorAttribute()
            {
                View = "~/Views/Shared/Error.cshtml"
            });
        }
        public void ExceptionTypePropertyWithNonExceptionTypeThrows()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            Assert.Throws<ArgumentException>(
                delegate { attr.ExceptionType = typeof(string); },
                "The type 'System.String' does not inherit from Exception.");
        }
示例#21
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            HandleErrorAttribute myHandleErrorAttribute = new HandleErrorAttribute();

            myHandleErrorAttribute.View          = "DatabaseError";
            myHandleErrorAttribute.ExceptionType = typeof(EntityException);
            myHandleErrorAttribute.Order         = 1;
            filters.Add(myHandleErrorAttribute);

            filters.Add(new HandleErrorAttribute());
        }
示例#22
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            HandleErrorAttribute handleErrorAttributeDB = new HandleErrorAttribute();

            handleErrorAttributeDB.View          = "DatabaseError";
            handleErrorAttributeDB.ExceptionType = typeof(System.Data.Entity.Core.EntityException);
            handleErrorAttributeDB.Order         = 1;

            filters.Add(handleErrorAttributeDB);
            filters.Add(new HandleErrorAttribute());
            filters.Add(new StatistiekActionFilter());
        }
        public void ErrorHandlingDoesNotFireIfCalledInChildAction()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();
            Mock<ExceptionContext> context = new Mock<ExceptionContext>();
            context.Setup(c => c.IsChildAction).Returns(true);

            // Act
            attr.OnException(context.Object);

            // Assert
            Assert.IsType<EmptyResult>(context.Object.Result);
        }
示例#24
0
        /// <summary>
        /// Set the list of filters to apply every time
        /// </summary>
        /// <param name="filters"></param>
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            HandleErrorAttribute handleError = new HandleErrorAttribute()
            {
                View = "Error"
            };

            filters.Add(handleError);

            StatusFilterAttribute statusFilter = new StatusFilterAttribute();

            filters.Add(statusFilter);
        }
        public void ExceptionTypeProperty() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act
            Type origType = attr.ExceptionType;
            attr.ExceptionType = typeof(SystemException);
            Type newType = attr.ExceptionType;

            // Assert
            Assert.AreEqual(typeof(Exception), origType);
            Assert.AreEqual(typeof(SystemException), attr.ExceptionType);
        }
示例#26
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            HandleErrorAttribute monErreur = new HandleErrorAttribute();

            monErreur.View = "Erreur2";
            filters.Add(monErreur);

            AuthorizeAttribute auth = new AuthorizeAttribute();
            //auth.Users = "WINDEV1610EVAL\\user";
            // auth.Roles="BUILTIN\\Administrators";

            //LanguageAttribute langue = new LanguageAttribute("fr-CA");
            //filters.Add(langue);
        }
        public void OnException()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute()
            {
                View          = "SomeView",
                Master        = "SomeMaster",
                ExceptionType = typeof(ArgumentException)
            };
            Exception exception = new ArgumentNullException();

            Mock <HttpContextBase> mockHttpContext = new Mock <HttpContextBase>(MockBehavior.Strict);

            mockHttpContext.Setup(c => c.IsCustomErrorEnabled).Returns(true);
            mockHttpContext.Setup(c => c.Session).Returns((HttpSessionStateBase)null);
            mockHttpContext.Setup(c => c.Response.Clear()).Verifiable();
            mockHttpContext.SetupSet(c => c.Response.StatusCode             = 500).Verifiable();
            mockHttpContext.SetupSet(c => c.Response.TrySkipIisCustomErrors = true).Verifiable();

            TempDataDictionary tempData   = new TempDataDictionary();
            IViewEngine        viewEngine = new Mock <IViewEngine>().Object;
            Controller         controller = new Mock <Controller>().Object;

            controller.TempData = tempData;

            ExceptionContext context = GetExceptionContext(
                mockHttpContext.Object,
                controller,
                exception
                );

            // Exception
            attr.OnException(context);

            // Assert
            mockHttpContext.Verify();
            ViewResult viewResult = context.Result as ViewResult;

            Assert.NotNull(viewResult);
            Assert.Equal(tempData, viewResult.TempData);
            Assert.Equal("SomeView", viewResult.ViewName);
            Assert.Equal("SomeMaster", viewResult.MasterName);

            HandleErrorInfo viewData = viewResult.ViewData.Model as HandleErrorInfo;

            Assert.NotNull(viewData);
            Assert.Same(exception, viewData.Exception);
            Assert.Equal("SomeController", viewData.ControllerName);
            Assert.Equal("SomeAction", viewData.ActionName);
        }
        public void ErrorHandlingDoesNotFireIfCalledInChildAction()
        {
            // Arrange
            HandleErrorAttribute    attr    = new HandleErrorAttribute();
            Mock <ExceptionContext> context = new Mock <ExceptionContext>();

            context.Setup(c => c.IsChildAction).Returns(true);

            // Act
            attr.OnException(context.Object);

            // Assert
            Assert.IsType <EmptyResult>(context.Object.Result);
        }
示例#29
0
        /// <summary>
        /// Registers global filters.
        /// </summary>
        /// <param name="filters">The global filters.</param>
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            // Create the handle error attribute for MVC error handling
            var handleErrorAttribute = new HandleErrorAttribute
            {
                // No Type specification == handle all exceptions
                View = "Exception",
            };

            filters.Add(handleErrorAttribute);

            // Create a new action attribute that sets the ViewBag PageTitle based on the route parameters
            filters.Add(new PageTitleAttribute());
        }
        public void OnExceptionWithCustomErrorsDisabledDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr    = new HandleErrorAttribute();
            ActionResult         result  = new EmptyResult();
            ExceptionContext     context = GetExceptionContext(GetHttpContext(false), null, new Exception());

            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged.");
        }
示例#31
0
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //準備一個HandleErrorAttribute
            var handleError = new HandleErrorAttribute();

            // 指定HandleError導向的頁面,要指定值  但會吃config的頁面
            handleError.View = "/Home";
            // 設定HandleError啟用
            handleError.Order = 0;
            //加到全域的filter中
            GlobalFilters.Filters.Add(handleError);
        }
示例#32
0
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //all site error handle
            filters.Add(new HandleErrorAttribute());
            filters.Add(new AuthorizeAttribute());



            //all site DataBase error handle  for databse exception
            HandleErrorAttribute dbException = new HandleErrorAttribute();

            dbException.ExceptionType = typeof(DbUpdateException);
            dbException.View          = "DatabaseExeptionView";
            filters.Add(dbException);
        }
示例#33
0
        public void ExceptionTypeProperty()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act
            Type origType = attr.ExceptionType;

            attr.ExceptionType = typeof(SystemException);
            Type newType = attr.ExceptionType;

            // Assert
            Assert.Equal(typeof(Exception), origType);
            Assert.Equal(typeof(SystemException), attr.ExceptionType);
        }
示例#34
0
        public void OnExceptionWithNon500ExceptionDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr    = new HandleErrorAttribute();
            ActionResult         result  = new EmptyResult();
            ExceptionContext     context = GetExceptionContext(GetHttpContext(), null, new HttpException(404, "Some Exception"));

            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.Same(result, context.Result);
        }
        public void OnExceptionWithNon500ExceptionDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr    = new HandleErrorAttribute();
            ActionResult         result  = new EmptyResult();
            ExceptionContext     context = GetExceptionContext(GetHttpContext(), null, new HttpException(404, "Some Exception"));

            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged.");
        }
示例#36
0
        public void OnExceptionWithCustomErrorsDisabledDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr    = new HandleErrorAttribute();
            ActionResult         result  = new EmptyResult();
            ExceptionContext     context = GetExceptionContext(GetHttpContext(false), null, new Exception());

            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.Same(result, context.Result);
        }
示例#37
0
        public void OnExceptionWithWrongExceptionTypeDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute()
            {
                ExceptionType = typeof(ArgumentException)
            };
            ActionResult     result  = new EmptyResult();
            ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new InvalidCastException());

            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.Same(result, context.Result);
        }
        public void OnException() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute() {
                View = "SomeView",
                Master = "SomeMaster",
                ExceptionType = typeof(ArgumentException) 
            };
            Exception exception = new ArgumentNullException();

            Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>(MockBehavior.Strict);
            mockHttpContext.Expect(c => c.IsCustomErrorEnabled).Returns(true);
            mockHttpContext.Expect(c => c.Session).Returns((HttpSessionStateBase)null);
            mockHttpContext.Expect(c => c.Response.Clear()).Verifiable();
            mockHttpContext.ExpectSet(c => c.Response.StatusCode, 500).Verifiable();
            mockHttpContext.ExpectSet(c => c.Response.TrySkipIisCustomErrors, true).Verifiable(); 
            
            TempDataDictionary tempData = new TempDataDictionary();
            IViewEngine viewEngine = new Mock<IViewEngine>().Object;
            Controller controller = new Mock<Controller>().Object;
            controller.TempData = tempData;

            ExceptionContext context = GetExceptionContext(mockHttpContext.Object, controller, exception);

            // Exception
            attr.OnException(context);

            // Assert
            mockHttpContext.Verify();
            ViewResult viewResult = context.Result as ViewResult;
            Assert.IsNotNull(viewResult, "The Result property should have been set to an instance of ViewResult.");
            Assert.AreEqual(tempData, viewResult.TempData);
            Assert.AreEqual("SomeView", viewResult.ViewName);
            Assert.AreEqual("SomeMaster", viewResult.MasterName);

            HandleErrorInfo viewData = viewResult.ViewData.Model as HandleErrorInfo;
            Assert.IsNotNull(viewData, "The ViewData model should have been set to an instance of ExceptionViewData.");
            Assert.AreSame(exception, viewData.Exception);
            Assert.AreEqual("SomeController", viewData.ControllerName);
            Assert.AreEqual("SomeAction", viewData.ActionName);
        }
        public void OnExceptionWithNullFilterContextThrows() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                delegate {
                    attr.OnException(null /* filterContext */);
                }, "filterContext");
        }
        public void ViewProperty() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "View", "Error", false /* testDefaultValue */, true /* allowNullAndEmpty */, "Error");
        }
        public void OnExceptionWithExceptionHandledDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();
            ActionResult result = new EmptyResult();
            ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new Exception());
            context.Result = result;
            context.ExceptionHandled = true;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.Same(result, context.Result);
        }
        public void OnExceptionWithNon500ExceptionDoesNothing()
        {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();
            ActionResult result = new EmptyResult();
            ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new HttpException(404, "Some Exception"));
            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.Same(result, context.Result);
        }
        public void OnExceptionWithWrongExceptionTypeDoesNothing() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute() { ExceptionType = typeof(ArgumentException) };
            ActionResult result = new EmptyResult();
            ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new InvalidCastException());
            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged.");
        }
        public void OnExceptionWithCustomErrorsDisabledDoesNothing() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();
            ActionResult result = new EmptyResult();
            ExceptionContext context = GetExceptionContext(GetHttpContext(false), null, new Exception());
            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged.");
        }
        public void OnExceptionWithNon500ExceptionDoesNothing() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();
            ActionResult result = new EmptyResult();
            ExceptionContext context = GetExceptionContext(GetHttpContext(), null, new HttpException(404, "Some Exception"));
            context.Result = result;

            // Exception
            attr.OnException(context);

            // Assert
            Assert.AreSame(result, context.Result, "The context's Result property should have remain unchanged.");
        }
        public void ViewProperty() {
            // Arrange
            HandleErrorAttribute attr = new HandleErrorAttribute();

            // Act & Assert
            MemberHelper.TestStringProperty(attr, "View", "Error", nullAndEmptyReturnValue: "Error");
        }