Пример #1
0
 public static String ToHtml(this Controller controller, ViewType viewType, String viewPath, Object model)
 {
     using (var sw = new StringWriter())
     {
         controller.Render(viewType, viewPath, model, sw);
         return sw.ToString();
     }
 }
Пример #2
0
 public static void Render(this Controller controller, ViewType viewType, String viewPath, Object model, TextWriter writer)
 {
     var cx = controller.ControllerContext;
     if (cx == null)
     {
         cx = CreateControllerContext(controller);
     }
     ViewEngineResult viewEngineResult = null;
     switch (viewType)
     {
         case ViewType.View: viewEngineResult = ViewEngines.Engines.FindView(cx, viewPath, null); break;
         case ViewType.PartialView: viewEngineResult = ViewEngines.Engines.FindPartialView(cx, viewPath); break;
         default: throw new InvalidOperationException();
     }
     if (viewEngineResult == null)
     {
         throw new InvalidOperationException("View not found");
     }
     var view = viewEngineResult.View;
     cx.Controller.ViewData.Model = model;
     var ctx = new ViewContext(cx, view, cx.Controller.ViewData, cx.Controller.TempData, writer);
     view.Render(ctx, writer);
 }
Пример #3
0
        private static string ReadViewHtml(string filePath, ViewType viewType)
        {
            filePath = filePath.TrimLine();

            switch (viewType)
            {
                case ViewType.PartialView:
                    if (!filePath.EndsWith(PartialViewExtension, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidOperationException(Format(Resources.InvalidPartialViewExtension, filePath));
                    }
                    break;
                case ViewType.Layout:
                    if (!filePath.EndsWith(LayoutViewExtension, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidOperationException(Format(Resources.InvalidLayoutViewExtension, filePath));
                    }
                    break;
                default:
                    if (!filePath.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidOperationException(Format(Resources.InvalidViewExtension, filePath));
                    }
                    break;
            }

            return File.ReadAllText(filePath, Encoding.UTF8).TrimLine();
        }