What is wrong with the commenting in cshtml when using any razor code, razor page, blazor etc..

lm1212 141 Reputation points
2022-09-20T08:47:31.847+00:00

From what I can tell so far, there is no way to comment out certain lines of code/sections in a razor page / component or cshtml page.
It does not seem to matter what I do in the file, there are times when the code just will not comment out !
And I cannot locate the rules for this or note any consistency, in what it will and won't comment out.
For example trying to comment out line 72 here ( @Aidan Wick { ) just does not seem to be possible.

    @*Page 1049*@  
  
<TableTemplate RowType="Person" RowData="People">  
    <Header>  
    <tr><th>ID</th><th>Name</th><th>Dept</th><th>Location</th></tr>  
    </Header>  
    <RowTemplate Context="p">  
        <td>@p.PersonId</td>  
        <td>@p.Surname, @p.Firstname</td>  
        <td>@p.Department?.Name</td>  
        <td>@p.Location?.City, @p.Location?.State</td>  
    </RowTemplate>  
</TableTemplate>  
  
@code {  
    [Inject]  
    public DataContext? Context { get; set; }  
    public IEnumerable<Person>? People => Context?.People  
    .Include(p => p.Department)  
    .Include(p => p.Location);  
}  
  
@*Page 1029*@  
@*<table class="table table-sm table-bordered table-striped">  
    <thead>  
        <tr>  
            <th>ID</th>  
            <th>Name</th>  
            <th>Dept</th>  
            <th>Location</th>  
        </tr>  
    </thead>  
    <tbody>  
        @foreach (Person p in People ?? Enumerable.Empty<Person>())  
        {  
            <tr class="@GetClass(p?.Location?.City)">  
                <td>@p?.PersonId</td>  
                <td>@p?.Surname, @p?.Firstname</td>  
                <td>@p?.Department?.Name</td>  
                <td>@p?.Location?.City, @p?.Location?.State</td>  
            </tr>  
        }  
    </tbody>  
</table>*@  
@*Page 1045*@  
@*<TableTemplate>  
    <Header>  
    <tr><th>ID</th><th>Name</th><th>Dept</th><th>Location</th></tr>  
    </Header>  
    <Body>  
        @foreach (Person p in People ?? Enumerable.Empty<Person>())  
        {  
        <tr class="@GetClass(p?.Location?.City)">  
            <td>@p?.PersonId</td>  
            <td>@p?.Surname, @p?.Firstname</td>  
            <td>@p?.Department?.Name</td>  
            <td>@p?.Location?.City, @p?.Location?.State</td>  
        </tr>  
        }  
    </Body>  
</TableTemplate>  
  
<ThemeWrapper Theme="info" Title="Location Selector">  
    <SelectFilter values="@Cities" title="@SelectTitle"  
                  @bind-SelectedValue="SelectedCity" />  
    <button class="btn btn-primary mt-2" @onclick="@(() => SelectedCity = "Oakland")"> Change</button>  
</ThemeWrapper>*@  
  
@*<SelectFilter values="@Cities" title="@SelectTitle" @bind-SelectedValue="SelectedCity" />  
*@  
  
// @code {  
//    [Inject]  
//    public DataContext? Context { get; set; }  
  
//    public IEnumerable<Person>? People => Context?.People  
//    .Include(p => p.Department).Include(p => p.Location).Take(ItemCount);  
  
//    public IEnumerable<string>? Cities => Context?.Locations.Select(l => l.City);  
  
//    public string SelectedCity { get; set; } = string.Empty;  
//    public string GetClass(string? city) =>  
//    SelectedCity == city ? "bg-info text-white" : "";  
  
//    [Parameter]  
//    public int ItemCount { get; set; } = 4;  
//    [Parameter]  
//    public string? SelectTitle { get; set; }  
      
  
//    public void HandleCustom(string newValue)  
//    {  
//        SelectedCity = newValue;  
//    }  
//}  
  
//<table class="table table-sm table-bordered table-striped">  
//    <thead>  
//        <tr>  
//            <th>ID</th>  
//            <th>Name</th>  
//            <th>Dept</th>  
//            <th>Location</th>  
//        </tr>  
//    </thead>  
//    <tbody>  
//        @foreach (Person p in People ?? Enumerable.Empty<Person>())  
//        {  
//            <tr class="@GetClass(p?.Location?.City)">  
//                <td>@p?.PersonId</td>  
//                <td>@p?.Surname, @p?.Firstname</td>  
//                <td>@p?.Department?.Name</td>  
//                <td>@p?.Location?.City, @p?.Location?.State</td>  
//            </tr>  
//        }  
//    </tbody>  
//</table>  
//<div class="form-group">  
//    <label for="city">City</label>  
//    <select name="city" class="form-control" @bind="SelectedCity">  
//        <option disabled selected value="">Select City</option>  
//        @foreach (string city in Cities ?? Enumerable.Empty<string>())  
//        {  
//            <option value="@city" selected="@(city == SelectedCity)">  
//                @city  
//            </option>  
//        }  
//    </select>  
//</div>  
//@code {  
//    [Inject]  
//    public DataContext? Context { get; set; }  
//    public IEnumerable<Person>? People =>  
//    Context?.People.Include(p => p.Department).Include(p => p.Location);  
//    public IEnumerable<string>? Cities => Context?.Locations.Select(l => l.City);  
//    public string SelectedCity { get; set; } = string.Empty;  
//    public string GetClass(string? city) =>  
//    SelectedCity == city ? "bg-info text-white" : "";  
//}  

The commenting in these files is an absolute nightmare !!

I have tried just about every way imaginable to comment out certain lines, including commenting out the document piece by piece, as the page
will not let me comment out the whole flle in one go.

Trying to find a way around this, is very tedious, annoying and time consuming. I just want the section commented out so I can continue with the app i am building!
I am having to continually place the old code in text file and store it elsewhere, instead of just highlighting and clicking comment out. Then I also have to go and fetch
the code later, and I cannot compare it with my current script, unless I want to maintain yet another file in my folder !

If

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,161 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-09-21T03:15:36.337+00:00

    Hi @lm1212

    Asp.net core Razor supports C# and HTML comments:

    @{  
        /* C# comment */  
        // Another C# comment  
    }  
    <!-- HTML comment -->  
    

    The code renders the following HTML:

    <!-- HTML comment -->  
    

    Razor comments are removed by the server before the webpage is rendered. Razor uses @* *@ to delimit comments.

    @*  
        @{  
            /* C# comment */  
            // Another C# comment  
        }  
        <!-- HTML comment -->  
    *@  
    

    More detail information about the Razor syntax, see Razor Comments.

    In your code, since you are using TableTemplate, I assume it is a Blazor component, right? The Blazor components also use Razor syntax.

    So, to comment the @code { }, we can use @* *@.

    For example trying to comment out line 72 here ( @Aidan Wick { ) just does not seem to be possible.

    For your example page, you can remove the // and use @* *@. After that clean and rebuild the project. Check the following screenshot:

    243209-image.png

    You can view the source code from here: 243218-sourcecode.txt

    Besides, the Blazor component support Partial class, for the code in the @code blocks, we can create a partial class and place the code in the partial class (.cs) file. Then, in the partial class file, we can use // to add comments, and in the .Razor file, we can add comments use @* *@.

    243264-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful