Crowbar.CrowbarViewContext.FindViewEngineResult C# (CSharp) Method

FindViewEngineResult() public method

Finds a view based on the specified view name.
public FindViewEngineResult ( System.Web.Mvc.ControllerContext controllerContext ) : System.Web.Mvc.ViewEngineResult
controllerContext System.Web.Mvc.ControllerContext The controller context.
return System.Web.Mvc.ViewEngineResult
        public virtual ViewEngineResult FindViewEngineResult(ControllerContext controllerContext)
        {
            Ensure.NotNull(controllerContext, "controllerContext");

            string name = ViewName.StartsWith("~")
                ? ViewName.Substring(ViewName.LastIndexOf("/") + 1)
                : ViewName;

            bool isPartialView = name.StartsWith("_");

            var viewEngineResult = isPartialView
                ? ViewEngines.Engines.FindPartialView(controllerContext, ViewName)
                : ViewEngines.Engines.FindView(controllerContext, ViewName, string.Empty);

            if (viewEngineResult == null)
            {
                string message = "The view was not found.";
                throw new ArgumentException(message, ViewName);
            }

            return viewEngineResult;
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Renders a partial view to a string in the specified context.
        /// </summary>
        /// <param name="crowbarViewContext">The view context.</param>
        /// <param name="viewModel">The view model.</param>
        /// <param name="cookies">Any cookies that were captures as part of the response.</param>
        /// <returns>The view rendered as a string.</returns>
        public static string ToString(CrowbarViewContext crowbarViewContext, object viewModel, out HttpCookieCollection cookies)
        {
            string viewName = crowbarViewContext.ViewName;

            using (var writer = new StringWriter())
            {
                var httpRequest = new HttpRequest("", "http://www.example.com", "");
                var httpResponse = new HttpResponse(writer);

                var controllerContext = CreateControllerContext(httpRequest, httpResponse, crowbarViewContext);
                var viewEngineResult = crowbarViewContext.FindViewEngineResult(controllerContext);

                var view = viewEngineResult.View;
                if (view == null)
                {
                    var locations = new StringBuilder();
                    foreach (string searchedLocation in viewEngineResult.SearchedLocations)
                    {
                        locations.AppendLine();
                        locations.Append(searchedLocation);
                    }

                    throw new ArgumentException("The view was not found. The following locations were searched: " + locations, viewName);
                }

                try
                {
                    var viewData = new ViewDataDictionary(viewModel);
                    var tempData = new TempDataDictionary();

                    var viewContext = new ViewContextStub(controllerContext, view, viewData, tempData, writer)
                    {
                        ClientValidationEnabled = crowbarViewContext.ClientValidationEnabled,
                        UnobtrusiveJavaScriptEnabled = crowbarViewContext.UnobtrusiveJavaScriptEnabled
                    };

                    view.Render(viewContext, httpResponse.Output);
                    cookies = controllerContext.HttpContext.Response.Cookies;

                    httpResponse.Flush();
                }
                finally
                {
                    viewEngineResult.ViewEngine.ReleaseView(controllerContext, view);
                }

                return writer.ToString();
            }
        }