コード例 #1
0
        private string GetViewContent <T>(string viewName, T model, string layoutName = "_Layout")
        {
            if ("/".Equals(viewName))
            {
                viewName = "/" + GlobalConstants.HomeIndex;
            }

            var fileName = viewName;

            if (!viewName.StartsWith("/"))
            {
                fileName = "/" + viewName;
            }

            var currentFileName = $"{GlobalConstants.View}{fileName}{GlobalConstants.Html}";

            var fileHtml = System.IO.File.ReadAllText(currentFileName);

            var layoutHtml = System.IO.File.ReadAllText($"{GlobalConstants.View}/{layoutName}{GlobalConstants.Html}");

            var allContent = layoutHtml.Replace(ContentPlaceholder, fileHtml);

            var cSharpContent = ViewEngine.GetHtml(layoutName, allContent, model, User);

            return(cSharpContent);
        }
コード例 #2
0
ファイル: Controller.cs プロジェクト: yotkoKanchev/csharp-web
        private HttpResponse ViewByName <T>(string viewPath, object viewModel)
        {
            IViewEngine viewEngine = new ViewEngine();
            var         html       = File.ReadAllText(viewPath);

            html = viewEngine.GetHtml(html, viewModel, this.User);
            var layout         = File.ReadAllText("Views/Shared/_Layout.html");
            var bodyWithLayout = layout.Replace("@RenderBody()", html);

            bodyWithLayout = viewEngine.GetHtml(bodyWithLayout, viewModel, this.User);

            return(new HtmlResponse(bodyWithLayout));
        }
コード例 #3
0
        private HttpResponse ViewByName <T>(string viewPath, object viewModel)
        {
            IViewEngine viewEngine = new ViewEngine();
            string      html       = File.ReadAllText(viewPath);

            html = viewEngine.GetHtml(html, viewModel, this.User); // viewEngine.GetHtml returns the final parsed html

            string layout         = File.ReadAllText("Views/Shared/_Layout.html");
            string bodyWithLayout = layout.Replace("@RenderBody()", html);             // Replacing the RenderBody() in _Layout.html with the html body

            bodyWithLayout = viewEngine.GetHtml(bodyWithLayout, viewModel, this.User); // viewEngine.GetHtml returns the final parsed html for the layout

            return(new HtmlResponse(bodyWithLayout));
        }
コード例 #4
0
        protected HttpResponse View <T>(T viewModel = null, [CallerMemberName] string viewName = null)
            where T : class
        {
            IViewEngine viewEngine = new ViewEngine();

            var controllerName = this.GetType().Name.Replace("Controller", string.Empty);
            var html           = File.ReadAllText("Views/" + controllerName + "/" + viewName + ".html");

            viewEngine.GetHtml(html, viewModel);
            var layout         = File.ReadAllText("Views/Shared/_Layout.html");
            var bodyWithLayout = layout.Replace("@RenderBody()", html);

            bodyWithLayout = viewEngine.GetHtml(bodyWithLayout, viewModel);
            return(new HtmlResponse(bodyWithLayout));
        }