asp.netweb-configcompilationcompilationmode

ASP.NET compilationMode Auto vs Never


Let's assume my ASPX page has no inline C# code blocks.

So, I can safely set

<pages compilationMode="Never" />

...in my web.config file and not worry about compilation errors.

Performance-wise, would there be any penalty to using the following setting?

<pages compilationMode="Auto" />

i.e. does the "auto" detection take any significant amount of time?


Solution

  • The impact of Auto appears to be very little. (Although obviously more than Never).

    If we examine the code in System.Web.UI.TemplateParser, we see in ImportSourceFile that process is aborted early if mode is set to Never:

    if (this.CompilationMode == CompilationMode.Never)
    {
        return null;
    }
    

    Which is of course helpful, and definitely the lowest-impact. However, continuing through the routines in TemplateParser, we can see in ParseStringInternal the parser literally scans the loaded template searching for variations of <%:

    if (!this.flags[2] && (match = BaseParser.aspCodeRegex.Match(text, startat)).Success)
    {
        string str3 = match.Groups["code"].Value.Trim();
        if (str3.StartsWith("$", StringComparison.Ordinal))
        {
            this.ProcessError(SR.GetString("ExpressionBuilder_LiteralExpressionsNotAllowed", new object[] { match.ToString(), str3 }));
        }
        else
        {
            this.ProcessCodeBlock(match, CodeBlockType.Code, text);
        }
    }
    

    Note the BaseParser.aspCodeRegex, which is an instance of this pattern:

    public AspCodeRegex()
    {
        base.pattern = @"\G<%(?!@)(?<code>.*?)%>";
        ...
    }
    

    If it encounters none, it simply moves on. The searching is a fairly inexpensive operation - the biggest hit is when code blocks are actually found, compiling them.