示例#1
0
        public MvcActionHelper(
            ControllerContext currentControllerContext,
            [AspMvcAction] string actionName = null,
            [AspMvcController] string controllerName = null,
            string httpMethod = "GET",
            RouteValueDictionary routeValues = null,
            string protocol = null,
            string hostName = null)
        {
            this.CurrentControllerContext = currentControllerContext;

            this.ActionName = actionName ?? this.CurrentControllerContext.RouteData.GetRequiredString("action");
            this.ControllerName = controllerName ?? this.CurrentControllerContext.RouteData.GetRequiredString("controller");

            this.HttpMethod = httpMethod;

            var httpContext = new MvcHelper.MockHttpContext
            {
                Request2 =
                    new MvcHelper.MockHttpRequest(this.CurrentControllerContext.HttpContext.Request)
                    {
                        HttpMethod2 = this.HttpMethod,
                        Url2 = this.Uri,
                    }
            };

            // Building route data.
            var urlHelper = new UrlHelper(this.CurrentControllerContext.RequestContext);
            var currentUri = this.CurrentControllerContext.RequestContext.HttpContext.Request.Url;
            this.Uri = new Uri(urlHelper.Action(
                this.ActionName,
                this.ControllerName,
                routeValues,
                protocol ?? currentUri.Scheme,
                hostName ?? currentUri.Host));

            var routeData = RouteTable.Routes.GetRouteData(httpContext);

            // Creating controller.
            this.Controller = (ControllerBase)this.ControllerFactory
                .CreateController(
                // note: the area does not affect which controller is selected
                new RequestContext(httpContext, routeData),
                this.ControllerName);

            this.ControllerType = this.Controller.GetType();

            this.ControllerDescriptor = new ReflectedControllerDescriptor(this.ControllerType);

            // Creating fake controller context.
            this.MockControllerContext = new ControllerContext(
                httpContext,
                routeData,
                this.Controller);

            this.Controller.ControllerContext = this.MockControllerContext;

            this.ActionDescriptor = this.ControllerDescriptor
                .FindAction(this.MockControllerContext, this.ActionName);
        }
示例#2
0
        /// <summary>
        /// Renders an embedded razor view.
        /// </summary>
        /// <param name="requesterType"> </param>
        /// <param name="viewName"></param>
        /// <param name="viewData"></param>
        /// <returns></returns>
        public static string RenderEmbeddedRazor(Type requesterType, [JetBrains.Annotations.AspMvcView] string viewName, ViewDataDictionary viewData)
        {
            // TODO: support layout page
            // TODO: support _ViewStart pages

            // Creating or getting the type that renders the view.
            var resourcePath = string.Format(
                "{0}\\Views\\{1}\\{2}.cshtml",
                Path.GetFileNameWithoutExtension(requesterType.Assembly.GetName().Name),
                requesterType.Name,
                viewName);

            var key = new RequesterTypeAndResourcePath
            {
                RequesterType = requesterType,
                ResourcePath  = resourcePath,
            };

            var type = pageTypeByResourcePath.GetOrAdd(key, CreateTypeForEmbeddedRazor);

            if (type == null)
            {
                throw new Exception("View not found.");
            }

            // Rendering the page, using the passed model.
            var page = (WebViewPage)Activator.CreateInstance(type);

            using (var stringWriter = new StringWriter())
            {
                // We need to emulate the environment of a web-request to render the view.
                var httpContext = new MvcHelper.MockHttpContext {
                    Request2 = new MvcHelper.MockHttpRequest()
                };
                var requestContext = new RequestContext(httpContext, new RouteData());

                page.ViewContext = new ViewContext(
                    new ControllerContext(requestContext, new HomeController()),
                    new RazorView(new ControllerContext(), resourcePath, null, true, new[] { "cshtml", "vbhtml" }),
                    viewData,
                    new TempDataDictionary(),
                    stringWriter);

                page.ViewData = viewData;
                page.Url      = new UrlHelper(requestContext, RouteTable.Routes);

                page.PushContext(new WebPageContext(httpContext, null, viewData), stringWriter);
                try
                {
                    // Executing the renderer, so that it writes to the stringWriter.
                    page.Execute();

                    // after executing the page, we need to copy value from the cloned ViewData
                    // to the original ViewData... so that the caller can see elements inserted
                    // or changed by the view
                    foreach (var eachViewDataItem in page.ViewData)
                    {
                        viewData[eachViewDataItem.Key] = eachViewDataItem.Value;
                    }
                }
                finally
                {
                    page.PopContext();
                }

                // The rendered page is inside the stringWriter.
                var result = stringWriter.GetStringBuilder().ToString();
                return(result);
            }
        }
示例#3
0
        /// <summary>
        /// Renders an embedded razor view.
        /// </summary>
        /// <param name="requesterType"> </param>
        /// <param name="viewName"></param>
        /// <param name="viewData"></param>
        /// <returns></returns>
        public static string RenderEmbeddedRazor(Type requesterType, [JetBrains.Annotations.AspMvcView] string viewName, ViewDataDictionary viewData)
        {
            // TODO: support layout page
            // TODO: support _ViewStart pages

            // Creating or getting the type that renders the view.
            var resourcePath = string.Format(
                "{0}\\Views\\{1}\\{2}.cshtml",
                Path.GetFileNameWithoutExtension(requesterType.Assembly.GetName().Name),
                requesterType.Name,
                viewName);

            var key = new RequesterTypeAndResourcePath
                {
                    RequesterType = requesterType,
                    ResourcePath = resourcePath,
                };

            var type = pageTypeByResourcePath.GetOrAdd(key, CreateTypeForEmbeddedRazor);
            if (type == null)
                throw new Exception("View not found.");

            // Rendering the page, using the passed model.
            var page = (WebViewPage)Activator.CreateInstance(type);
            using (var stringWriter = new StringWriter())
            {
                // We need to emulate the environment of a web-request to render the view.
                var httpContext = new MvcHelper.MockHttpContext { Request2 = new MvcHelper.MockHttpRequest() };
                var requestContext = new RequestContext(httpContext, new RouteData());

                page.ViewContext = new ViewContext(
                    new ControllerContext(requestContext, new HomeController()),
                    new RazorView(new ControllerContext(), resourcePath, null, true, new[] { "cshtml", "vbhtml" }),
                    viewData,
                    new TempDataDictionary(),
                    stringWriter);

                page.ViewData = viewData;
                page.Url = new UrlHelper(requestContext, RouteTable.Routes);

                page.PushContext(new WebPageContext(httpContext, null, viewData), stringWriter);
                try
                {
                    // Executing the renderer, so that it writes to the stringWriter.
                    page.Execute();

                    // after executing the page, we need to copy value from the cloned ViewData
                    // to the original ViewData... so that the caller can see elements inserted
                    // or changed by the view
                    foreach (var eachViewDataItem in page.ViewData)
                        viewData[eachViewDataItem.Key] = eachViewDataItem.Value;
                }
                finally
                {
                    page.PopContext();
                }

                // The rendered page is inside the stringWriter.
                var result = stringWriter.GetStringBuilder().ToString();
                return result;
            }
        }
        public MvcActionHelper(
            ControllerContext currentControllerContext,
            [AspMvcAction] string actionName         = null,
            [AspMvcController] string controllerName = null,
            string httpMethod = "GET",
            RouteValueDictionary routeValues = null,
            string protocol = null,
            string hostName = null)
        {
            this.CurrentControllerContext = currentControllerContext;

            this.ActionName     = actionName ?? this.CurrentControllerContext.RouteData.GetRequiredString("action");
            this.ControllerName = controllerName ?? this.CurrentControllerContext.RouteData.GetRequiredString("controller");

            this.HttpMethod = httpMethod;

            var httpContext = new MvcHelper.MockHttpContext
            {
                Request2 =
                    new MvcHelper.MockHttpRequest(this.CurrentControllerContext.HttpContext.Request)
                {
                    HttpMethod2 = this.HttpMethod,
                    Url2        = this.Uri,
                }
            };

            // Building route data.
            var urlHelper  = new UrlHelper(this.CurrentControllerContext.RequestContext);
            var currentUri = this.CurrentControllerContext.RequestContext.HttpContext.Request.Url;

            this.Uri = new Uri(urlHelper.Action(
                                   this.ActionName,
                                   this.ControllerName,
                                   routeValues,
                                   protocol ?? currentUri.Scheme,
                                   hostName ?? currentUri.Host));

            var routeData = RouteTable.Routes.GetRouteData(httpContext);

            // Creating controller.
            this.Controller = (ControllerBase)this.ControllerFactory
                              .CreateController(
                // note: the area does not affect which controller is selected
                new RequestContext(httpContext, routeData),
                this.ControllerName);

            this.ControllerType = this.Controller.GetType();

            this.ControllerDescriptor = new ReflectedControllerDescriptor(this.ControllerType);

            // Creating fake controller context.
            this.MockControllerContext = new ControllerContext(
                httpContext,
                routeData,
                this.Controller);

            this.Controller.ControllerContext = this.MockControllerContext;

            this.ActionDescriptor = this.ControllerDescriptor
                                    .FindAction(this.MockControllerContext, this.ActionName);
        }