ASP.NET MVC 3: Server-Side Comments with Razor

Earlier this week we shipped the ASP.NET MVC 3 Release Candidate.  It supports “go live” deployments, and includes a bunch of great improvements/enhancements on top of the features we enabled earlier with the ASP.NET MVC 3 beta and first preview releases.

This is another in a series of “mini-posts” I’m doing that talk about a few of the new ASP.NET MVC 3 Beta/RC features in more detail:

In today’s post I’m going to discuss a small, but useful, feature we’ve recently introduced: server-side comment support in Razor.

Why Server Side Comments?

Scenario: You are working within a view template, and want to temporarily disable some content or view code (potentially to help track down a problem, or to temporarily change the output of your site).

HTML supports client-side comments (<!-- -->), which you can use to prevent a browser from parsing/running/displaying the HTML content within it.  The problem with using this approach for many scenarios, though, is that the content contained within the comment is still sent down from the server to the client unnecessarily – and the server-side code within the comment will still execute on the server. 

ASP.NET Web Forms supports a server-side comment syntax (<%-- --%>) that you can use to completely disable content/code/controls within a page.  With server-side comments, the ASP.NET Web Forms compiler completely ignore everything within the <%-- --%> blocks at parse time, and removes the content completely when assembling the page (like the contents wasn’t there at all).  You can learn more about this feature from an older blog post of mine here.

One question someone asked me earlier this week was “is there an equivalent way to do this with Razor?”.  The answer is - yes!

Server Side Comments with Razor

Razor now allows you to wrap any code/content/region of a view within a @* comment *@ syntax (which works for both C# and VB templates).  This syntax indicates that the Razor parser should ignore everything within that block and treat it like it isn’t there at all (meaning nothing is ever executed, there is no performance overhead at runtime, and nothing is sent down to the client).

For example, below I’ve wrapped some HTML content and server-code within a @* *@ block.  This is equivalent to it not being there at all:

image

Notice above how Visual Studio (starting with this week’s ASP.NET MVC 3 RC build) now colorizes comments (by default in green) to make them more visible within the editor. 

Visual Studio also now enables you to use the comment/uncomment buttons on the text editor toolbar to easily comment/un-comment a block of text/code.  For example, we could select the above @* *@ comment block within the text editor, and then click the “Uncomment” toolbar button (or better yet press Ctrl+K, Ctrl+U) to have VS uncomment the region:

image

When we do this the editor will automatically uncomment the block for us (saving us some keystrokes):

image

We could then highlight another region of text/code within the editor, and then either click the “Comment” toolbar button or press Ctrl+K, Ctrl+C to apply a server-side comment:

image

When we do this the editor will automatically comment the block for us (saving us some keystrokes):

image

Summary

Server-side comments are a small, but useful, feature that can come in handy in a variety of circumstances.  Razor now enables server-side comments at runtime, and Visual Studio provides nice tooling support for them at development time.

Hope this helps,

Scott

P.S. In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu

20 Comments

  • Nice feature. Server-side comments also allow you to comment on inline-code if any, or list what you are planning to do in the near future. I just wonder when Razor documentation will appear on MSDN !

  • Just wondering if inline commenting is possible for example like this:

    @helper.someHelper(arg1, arg2, @* arg3, arg4, *@ arg5, arg6)

    So here args 3 and 4 should be ignored and only a total of 4 args are sent to someahelper.

    Also, second part of my question, is using * and * enough inline or do we use @* and *@ instead?
    Thanks

  • @Erx,

    >>>>>>>> Just wondering if inline commenting is possible for example like this:
    >>>>>>>> @helper someHelper(arg1, arg2, @* arg3, arg4, *@ arg5, arg6)
    >>>>>>>> So here args 3 and 4 should be ignored and only a total of 4 args are sent to someahelper.

    You can't use the server-side comment syntax within a code-block - as Razor will treat that as C#/VB and parse it as such. What you *can* do, though, is use a C# comment syntax within your helper. For example:

    @helper someHelper(arg1, arg2, /* arg3, arg4, */ arg5, arg6)

    The above /* */ comment works fine when nested within a helper method like above.

    >>>>>>>> Also, second part of my question, is using * and * enough inline or do we use @* and *@ instead?

    You need to use @* as opposed to just * - otherwise the parser can't distinguish them (since * is a valid C#/VB keyword as well as valid HTML one).

    Hope this helps,

    Scott

  • Hello Scott, I'm a Chinese programmer. How to use @RenderSection in Razor ? Thanks

  • In WebMatrix, when using Pages with the Razor syntax, what is the underlying framework, is it WebForms or MVC? In other words, is a System.Web.UI.Page class compiled, or is the file compiled into a Controller and a View?

    Just curious, and I can't find that information anywhere

    Thanks!

  • Hi Scott,

    another question:

    What is the significance of the fact that all Razor/WebMatrix assemblies start with Microsoft instead of System? Is it not a part of the .NET framework? Where do you draw the line, and how can we tell?

    If Mono wants to implement this, can they? Would they have to call their assemblies Mono.WebPages? And would that mean that it's not cross platform because the basic using statements are not portable?

    I think you should start blogging about openness, open-source and what it means to the .NET community.

    Thanks for answering and considering!

  • For those of you using the VerBose.NET language (also known as VB.NET), you will be happy to hear that Razor actually makes your code even MORE verbose:

    @Code
    Dim Variable As Integer = 42
    @End Code

    Happy coding/novel writing!

  • Is there a date for final release yet (MVC3)? I am considering using this for project that we need to roll out live early next year, does this seem a little reckless seeing it's only at RC stage?

  • it's nice feature,cool

  • Hi Scott,

    When trying this out the other day, I noticed that TODO comments aren't registered in the Task List. Is this going to work in the final release?

  • Hi Scott,

    Will there be line comments as in the beta?


    @// my comment
    list item


  • Why create a new comment format, why not use the already existing one from Web Forms?

  • Scott,

    I hope you will release the source for the Visual Studio tools and the Razor source codes also!

    Will you? :-)

    Thanks,
    Attila

  • Nice feature. Love it.

  • @Erana,

    Well Said! :)

  • Hi all,

    Am I completely missing something here? Why didn't the existing ASP.net commenting escape sequences get used?

    Why would I want to learn some other completely arbitrary convention when the existing standard is well known and well used.

    Unless I've missed something obvious this seems like complete silliness!

    S

  • Scott,

    @* *@ syntax it great! Less typing = more productivity.

  • @Erana

    While I don't want to start a language war - that is what is nice about .net, everyone can use the language they like - I want to to understand where you come from.

    I have been doing c# since it came out, and RARELY typecast. There are some times you have to, but it would be the same in VB.NET.

  • @Christian - Can you email razorvs@microsoft.com with the details of this? We'll do our best to figure out the issue there!

  • @Christian - your issue seems to be caused by the fact you haven't prefixed the line you want rendered to your JavaScript block with "@" (minus the quotes). You'll need to do this to ensure Razor knows to emit the results of the call to MvcHtmlString.Create() because as far as it knows you're still in code from the for loop construct.

    As to the editor giving you a warning, unfortunately we don't properly support server-side code blocks nested inside client-side JavaScript or CSS blocks today. It's something we're looking into for a future version.

    Thanks.

Comments have been disabled for this content.