Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mike 18 posts 39 karma points
    Feb 25, 2016 @ 11:32
    Mike
    0

    Stringbuilders and razor

    Hi. I have been recently been told that I should not be using StringBuilders in razor because they can't be cached and adversely affect server performance. Is this true? I understand that a 'purist' probably wouldn't put any c# code in razor but that seems to slightly negate the whole point of having razor. Can anyone offer advice please? Thanks

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 25, 2016 @ 19:39
    Dan Diplo
    1

    I'm pretty sure that is rubbish. Razor views get compiled to C# classes at run-time and are for all intents and purposes treated just like any other class or code. You can find the compiled classes in your Temporary ASP.NET Files folder. Here's an extract from a compiled master view:

    #pragma checksum "D:\Websites\Umbraco\V7 Test\UmbracoCms.7.3.7\Views\Master.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0C006E4C5DF307A523F60E07B8055DEB6E07B654"
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace ASP {
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Net;
        using System.Web;
        using System.Web.Helpers;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.WebPages;
        using System.Web.Mvc;
        using System.Web.Mvc.Ajax;
        using System.Web.Mvc.Html;
        using System.Web.Routing;
        using Umbraco.Web;
        using Umbraco.Core;
        using Umbraco.Core.Models;
        using Umbraco.Web.Mvc;
        using umbraco;
        using Examine;
        using WebMatrix.Data;
        using WebMatrix.WebData;
        using Microsoft.Web.Helpers;
    
    
        public class _Page_Views_Master_cshtml : Umbraco.Web.Mvc.UmbracoTemplatePage {
    
    #line hidden
    
            public _Page_Views_Master_cshtml() {
            }
    
            protected ASP.global_asax ApplicationInstance {
                get {
                    return ((ASP.global_asax)(Context.ApplicationInstance));
                }
            }
    
            public override void Execute() {
    
                #line 2 "D:\Websites\Umbraco\V7 Test\UmbracoCms.7.3.7\Views\Master.cshtml"
    
        Layout = null;
        var home = @CurrentPage.Site();
    
    
                #line default
                #line hidden
    WriteLiteral("\n\n<!DOCTYPE html>\n<html");
    
    WriteLiteral(" lang=\"en\"");
    
    WriteLiteral(">\n  <head>\n\n    <!-- Meta tags -->\n    <meta");
    
    WriteLiteral(" charset=\"utf-8\"");
    
    WriteLiteral(">\n    <meta");
    
    WriteLiteral(" http-equiv=\"X-UA-Compatible\"");
    
    WriteLiteral(" content=\"IE=edge\"");
    
    WriteLiteral(">\n    <meta");
    
    WriteLiteral(" name=\"viewport\"");
    
    WriteLiteral(" content=\"width=device-width, initial-scale=1\"");
    
    WriteLiteral(">\n    <meta");
    
    WriteLiteral(" name=\"description\"");
    
    WriteAttribute("content", Tuple.Create(" content=\"", 356), Tuple.Create("\"", 394)
    
                #line 15 "D:\Websites\Umbraco\V7 Test\UmbracoCms.7.3.7\Views\Master.cshtml"
    , Tuple.Create(Tuple.Create("", 366), Tuple.Create<System.Object, System.Int32>(CurrentPage.siteDescription
    
                #line default
                #line hidden
    , 366), false)
    );
    
    WriteLiteral(">\n\n    <title>");
    
    
                #line 17 "D:\Websites\Umbraco\V7 Test\UmbracoCms.7.3.7\Views\Master.cshtml"
          Write(CurrentPage.Name);
    
  • Mike 18 posts 39 karma points
    Feb 29, 2016 @ 10:13
    Mike
    0

    Thanks for the reply. It's an interesting one. So is it okay for me to use a string builder in razor to create, say, my navigation structure on each page? In theory each page would be slightly different, having a different 'on' page, so caching seems largely irrelevant for this anyway. Thanks!

  • M T 35 posts 212 karma points
    Feb 29, 2016 @ 10:31
    M T
    0

    Hi Mike

    The only downside of doing this is that if other people go through your code they might not expect to find string builders in a view. I always keep as much logic as I can in the controllers as I work as part of a team and it's good practice to keep clean and easy to read code!

Please Sign in or register to post replies

Write your reply to:

Draft