How do I disable or change auto-formatting in MVC Razor files?

I have an application where we are using a razor engine to return XML, here is an example of a partial view that I am currently working on:

@{   
    var includeNamespace = ViewData["includeNamespace"] != null && (bool)ViewData["includeNamespace"];
}
@if (Model != null)
{
    <Model @{if (includeNamespace){<text>xmlns="@Html.RenderNamespace()"</text>}}>
        <DateFieldOne>@Html.EncodeDate(Model.DateFieldOne)</DateFieldOne>
        <FieldTwo>@Html.Encode(Model.FieldTwo)</FieldTwo>
        @if (Model.FieldThree != null)
        {
            <FieldThreeCollection>
                @foreach (var fieldThree in Model.FieldThree)
                {
                    <FieldThree>
                        <SubFieldOne>@Html.Encode(fieldThree.SubFieldOne)</SubFieldOne>
                        <SubDateFieldTwo>@Html.EncodeDate(fieldThree.SubDateFieldTwo)</SubDateFieldTwo>
                        <SubFieldThree>@fieldThree.SubFieldThree</SubFieldThree>
                        <SubFieldFour>@fieldThree.SubFieldFour</SubFieldFour>
                    </FieldThree>
                }
            </FieldThreeCollection>
        }
        @if (Model.CanUpdate)
        {
            <link rel="Cancel" verb="POST" href="@Html.UriHelper().BuildUri("someEndpoint/{0}/cancel", Model.Id)"/>
        }
  </Model>
}

      

The strange behavior I see is when I inserted @{if (includeNamespace){<text>xmlns="@Html.RenderNamespace()"</text>}}

visual studio into the section , reformatted the view and removed all capitalization from the xml elements; example:

@{
    var includeNamespace = ViewData["includeNamespace"] != null && (bool)ViewData["includeNamespace"];
}
@if (Model != null)
{
    <model @{if (includeNamespace) { <text> xmlns="@Html.RenderNamespace()" </text> }}>
        <datefieldone>@Html.EncodeDate(Model.DateFieldOne)</datefieldone>
        <fieldtwo>@Html.Encode(Model.FieldTwo)</fieldtwo>
        @if (Model.FieldThree != null)
        {
            <fieldthreecollection>
                @foreach (var fieldThree in Model.FieldThree)
                {
                    <fieldthree>
                        <subfieldone>@Html.Encode(fieldThree.SubFieldOne)</subfieldone>
                        <subdatefieldtwo>@Html.EncodeDate(fieldThree.SubDateFieldTwo)</subdatefieldtwo>
                        <subfieldthree>@fieldThree.SubFieldThree</subfieldthree>
                        <subfieldfour>@fieldThree.SubFieldFour</subfieldfour>
                    </fieldthree>
                }
            </fieldthreecollection>
        }
        @if (Model.CanUpdate)
        {
            <link rel="Cancel" verb="POST" href="@Html.UriHelper().BuildUri("someEndpoint/{0}/cancel", Model.Id)" />
        }
    </model>
}

      

When I was typing this question, I realized that it did it because it expects html in which the elements are not capitalized, so now my question is, is there a way to stop Razor from doing this?

+3


source to share


2 answers


Visual Studio formats cshtml files as html by default. It is standard in html to use lowercase letters for the starting letter of tags. To change this behavior, you need to change the settings for all HTML documents.

From the Tools menu, open Options. Then on the left open "Text Editor / HTML / Advanced". The Format On Insert set is false.



Unfortunately, I cannot think of a way to disable this behavior for a single file.

+2


source


One option guides you to right-click the cshtml file in your solution and select "Open With ..." then "Source Editor (Text)" (close the file first).

A more permanent fix is ​​to open Options from the Tools menu, select Text Editor, then File Extension. Then enter "cshtml" in the Extension field and select "Script Editor" from the list, then click "Add" and then open the file again.



The problem with this approach is that you lose all intellisense capabilities.

+1


source







All Articles