public void GetPropertiesShouldNotThrowExceptionForNormalController()
        {
            MyMvc.IsUsingDefaultConfiguration();

            var helper = ControllerPropertyHelper.GetProperties<MvcController>();

            var controller = new MvcController();
            var controllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    RequestServices = TestServiceProvider.Global
                }
            };

            controller.ControllerContext = controllerContext;

            Assert.NotNull(controller.ControllerContext);
            Assert.Same(controllerContext, controller.ControllerContext);

            var gotControllerContext = helper.ControllerContextGetter(controller);

            Assert.NotNull(gotControllerContext);
            Assert.Same(gotControllerContext, controller.ControllerContext);

            Test.AssertException<InvalidOperationException>(
                () =>
                {
                    var gotActionContext = helper.ActionContextGetter(controller);
                },
                "ActionContext could not be found on the provided MvcController. The property should be specified manually by providing controller instance or using the specified helper methods.");
        }
 public void ReleaseController(ControllerContext context, object controller)
 {
     IDisposable dispose = controller as IDisposable;
     if (dispose != null)
     {
         dispose.Dispose();
     }
 }
 public static void Validate(this IObjectModelValidator objectModelValidator, ControllerContext controllerContext, object model)
 {
     objectModelValidator.Validate(
         controllerContext,
         validationState: null,
         prefix: string.Empty,
         model: model);
 }
 public object CreateController(ControllerContext context)
 {
     if (context.ActionDescriptor.ControllerTypeInfo.AsType() == typeof(LoginController))
     {
         return new LoginController(
             loginValidator: new TuduManayer.BussinessLogic.Login.LoginValidator());
     }
     return new HomeController();
 }
        public void GetPropertiesShouldNotThrowExceptionForPocoController()
        {
            MyMvc
                .IsUsingDefaultConfiguration()
                .WithServices(services =>
                {
                    services.AddHttpContextAccessor();
                });

            var helper = ViewFeaturesControllerPropertyHelper.GetViewFeatureProperties<FullPocoController>();

            var controllerContext = new ControllerContext();
            var controller = new FullPocoController
            {
                CustomControllerContext = controllerContext
            };

            Assert.NotNull(controller.CustomControllerContext);
            Assert.Same(controllerContext, controller.CustomControllerContext);

            var gotControllerContext = helper.ControllerContextGetter(controller);

            Assert.NotNull(gotControllerContext);
            Assert.Same(gotControllerContext, controller.CustomControllerContext);

            var actionContext = new ActionContext();

            controller.CustomActionContext = actionContext;

            Assert.NotNull(controller.CustomActionContext);
            Assert.Same(actionContext, controller.CustomActionContext);

            var gotActionContext = helper.ActionContextGetter(controller);

            Assert.NotNull(gotActionContext);
            Assert.Same(gotActionContext, controller.CustomActionContext);

            var gotViewData = helper.ViewDataGetter(controller);

            Assert.NotNull(gotViewData);
            Assert.Same(gotViewData, controller.CustomViewData);

            var gotTempData = helper.TempDataGetter(controller);

            Assert.NotNull(gotTempData);
            Assert.Same(gotTempData, controller.CustomTempData);

            MyMvc.IsUsingDefaultConfiguration();
        }
        public ModelBindingActionInvoker(
            ControllerActionInvokerCache cache,
            IControllerFactory controllerFactory,
            IControllerArgumentBinder controllerArgumentBinder,
            ILogger logger,
            DiagnosticSource diagnosticSource,
            ActionContext actionContext,
            IReadOnlyList<IValueProviderFactory> valueProviderFactories,
            int maxModelValidationErrors)
                : base(cache, controllerFactory, controllerArgumentBinder, logger, diagnosticSource, actionContext, valueProviderFactories, maxModelValidationErrors)
        {
            this.BoundActionArguments = new Dictionary<string, object>();

            this.controllerFactory = controllerFactory;
            this.controllerArgumentBinder = controllerArgumentBinder;

            this.controllerContext = new ControllerContext(actionContext);
            this.controllerContext.ModelState.MaxAllowedErrors = maxModelValidationErrors;
            this.controllerContext.ValueProviderFactories = new List<IValueProviderFactory>(valueProviderFactories);
        }
        public void GetPropertiesShouldNotThrowExceptionForNormalController()
        {
            MyApplication.StartsFrom<DefaultStartup>();

            var helper = ViewDataPropertyHelper.GetViewDataProperties<MvcController>();

            var controller = new MvcController();
            var controllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    RequestServices = TestServiceProvider.Global
                }
            };

            var gotViewData = helper.ViewDataGetter(controller);

            Assert.NotNull(gotViewData);
            Assert.Same(gotViewData, controller.ViewData);
        }
Beispiel #8
0
        public object BindModel(Microsoft.AspNetCore.Mvc.ControllerContext controllerContext, Microsoft.AspNetCore.Mvc.ModelBinding.ModelBindingContext bindingContext)
        {
            try
            {
                var request = controllerContext.HttpContext.Request;

                return(new GridSettings
                {
                    IsSearch = bool.Parse(request.Query["_search"].ToString() ?? "false"),
                    PageIndex = int.Parse(request.Query["page"].ToString() ?? "1"),
                    PageSize = int.Parse(request.Query["rows"].ToString() ?? "10"),
                    SortColumn = request.Query["sidx"].ToString() ?? "",
                    SortOrder = request.Query["sord"].ToString() ?? "asc",
                    Where = Filter.Create(request.Query["filters"].ToString() ?? "")
                });
            }
            catch
            {
                return(null);
            }
        }
        public void GetPropertiesShouldNotThrowExceptionForPocoController()
        {
            MyApplication
                .StartsFrom<DefaultStartup>()
                .WithServices(services =>
                {
                    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                });

            var helper = ControllerPropertyHelper.GetProperties<FullPocoController>();

            var controllerContext = new ControllerContext();
            var controller = new FullPocoController
            {
                CustomControllerContext = controllerContext
            };

            Assert.NotNull(controller.CustomControllerContext);
            Assert.Same(controllerContext, controller.CustomControllerContext);

            var gotControllerContext = helper.ControllerContextGetter(controller);

            Assert.NotNull(gotControllerContext);
            Assert.Same(gotControllerContext, controller.CustomControllerContext);

            var actionContext = new ActionContext();

            controller.CustomActionContext = actionContext;

            Assert.NotNull(controller.CustomActionContext);
            Assert.Same(actionContext, controller.CustomActionContext);

            var gotActionContext = helper.ActionContextGetter(controller);

            Assert.NotNull(gotActionContext);
            Assert.Same(gotActionContext, controller.CustomActionContext);

            MyApplication.StartsFrom<DefaultStartup>();
        }
Beispiel #10
0
        private static ViewEngineResult GetViewResult(ControllerContext controllerContext, string viewToRender, bool isPartial)
        {
            var engine        = controllerContext.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
            var getViewResult = engine.GetView(executingFilePath: null, viewPath: viewToRender, isMainPage: !isPartial);

            if (getViewResult.Success)
            {
                return(getViewResult);
            }
            var findViewResult = engine.FindView(controllerContext, viewToRender, isMainPage: !isPartial);

            if (findViewResult.Success)
            {
                return(findViewResult);
            }
            var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);

            var errorMessage = string.Join(
                Environment.NewLine,
                new[] { $"Unable to find view '{viewToRender}'. The following locations were searched:" }.Concat(searchedLocations));

            throw new Exception(errorMessage);
        }
        public void GetPropertiesShouldNotThrowExceptionForPocoController()
        {
            MyApplication
                .StartsFrom<DefaultStartup>()
                .WithServices(services =>
                {
                    services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                });

            var helper = TempDataPropertyHelper.GetTempDataProperties<FullPocoController>();

            var controllerContext = new ControllerContext();
            var controller = new FullPocoController
            {
                CustomControllerContext = controllerContext
            };
            
            var gotTempData = helper.TempDataGetter(controller);

            Assert.NotNull(gotTempData);
            Assert.Same(gotTempData, controller.CustomTempData);

            MyApplication.StartsFrom<DefaultStartup>();
        }
        public void WithCustomControllerContextShouldSetItToAccessor()
        {
            MyMvc
                .IsUsingDefaultConfiguration()
                .WithServices(services =>
                {
                    services.AddActionContextAccessor();
                });

            var actionDescriptor = new ControllerActionDescriptor { Name = "Test" };
            var actionContext = new ControllerContext { ActionDescriptor = actionDescriptor };

            MyMvc
                .Controller<ActionContextController>()
                .WithControllerContext(actionContext)
                .ShouldPassFor()
                .TheController(controller =>
                {
                    Assert.NotNull(controller);
                    Assert.NotNull(controller.Context);
                    Assert.Equal("Test", controller.Context.ActionDescriptor.Name);
                });

            MyMvc.IsUsingDefaultConfiguration();
        }
            public EventControllerBuilder WithUserLogged()
            {
                var httpContext = new Mock<HttpContext>();

                var controllerContext = new Mock<ControllerContext>();
                var principal = new Moq.Mock<ClaimsPrincipal>();


                principal.Setup(p => p.IsInRole("Administrator")).Returns(true);
                principal.SetupGet(x => x.Identity.Name).Returns("userName");

                httpContext.Setup(x => x.User).Returns(principal.Object);
                controllerContext.Object.HttpContext = httpContext.Object;
                _controllerContext = controllerContext.Object;

                return this;
            }
        public void WithCustomControllerContextShouldSetItToAccessor()
        {
            MyApplication
                .StartsFrom<DefaultStartup>()
                .WithServices(services =>
                {
                    services.AddActionContextAccessor();
                });

            var actionDescriptor = new ControllerActionDescriptor { DisplayName = "Test" };
            var actionContext = new ControllerContext { ActionDescriptor = actionDescriptor };

            MyController<ActionContextController>
                .Instance()
                .WithControllerContext(actionContext)
                .ShouldPassForThe<ActionContextController>(controller =>
                {
                    Assert.NotNull(controller);
                    Assert.NotNull(controller.Context);
                    Assert.Equal("Test", controller.Context.ActionDescriptor.DisplayName);
                });

            MyApplication.StartsFrom<DefaultStartup>();
        }
 /// <summary>
 /// Releases the controller.
 /// </summary>
 /// <param name="context">The Microsoft.AspNet.Mvc.ActionContext for the executing action.</param>
 /// <param name="controller">The controller instance.</param>
 public void Release(ControllerContext context, object controller)
 {
 }
 /// <summary>Creates a controller.</summary>
 /// <param name="context">The Microsoft.AspNet.Mvc.ActionContext for the executing action.</param>
 /// <returns>A new controller instance.</returns>
 public object Create(ControllerContext context) =>
     this.container.GetInstance(context.ActionDescriptor.ControllerTypeInfo.AsType());