What is the meaning of [ ] in Razor?

Posted on 2011-10-20
Last Modified: 2012-05-12
The default MVC3 web application has the following:

@if(Request.IsAuthenticated) {
    <text>Welcome <b>@Context.User.Identity.Name</b>!
    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
else {
    @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]

What does this do:
 [ @Html.ActionLink("Log Off", "LogOff", "Account") ]

then a second time with the @ outside the square brackets:
 @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]

Question by:newbieweb
    LVL 9

    Assisted Solution

    The square brackets would be rendered as it is.
    like this:
    Welcome (userName here)!  [Log Off]            ---for authenticated user.
    [Log On]                                                         ---for unauthenticated user.

    Author Comment

    Ok, then wahat is the meaning of:

    LVL 9

    Accepted Solution

    As you have  "@if(" in first code block, you don't require @: in this line:

    [ @Html.ActionLink("Log Off", "LogOff", "Account") ]

    while, [ @Html.ActionLink("Log On", "LogOn", "Account") ] line requires it as you are specifying this Code Nuggets within the static content.

    @: is used to define start of static content.

    In @: [,  [ is only static content. rendered as [ only.

    refer this for razor syntax introduction.

    Author Closing Comment


    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now