I've noticed that the Razor <text>
tag refuses to follow any formatting conventions. For example, when formatting a cshtml
document, I end up with code like the following:
@MyHelper.MyMethod(@<div>
<p>Hello world</p>
</div>)
@MyHelper.MyMethod(@<text>
<p>Hello world</p>
</text>)
Note that this isn't specific to Razor template functions either, <text>
blocks anywhere will do the same thing.
The client HTML tags (and their contents) such as <div>
follow the formatting style I've specified, however regardless of where I add (or remove) the <text>
element in the Tag Specific Options..., there is no effect. The desired result would be that the <text>
element contents be formatted like the <div>
element contents, in the previous example.
Are there any tricks, or work-arounds to enforcing the same formatting style on <text>
elements as that of other block-level client HTML elements (<div>
, <section>
, etc.)
I've seen other such formatting issues with the Razor language, such as here and here, so this isn't without precedent and it seems likely there may be no solution. Either way, maybe someone has come across a fix.
It's a Razor 2.X website, building in VS 2010
Addendum
(I've grown weary of this.)
Perhaps this issue has been solved in 2012; don't know, haven't updated. However, <text>
blocks, @
-prefixed blocks, and more do not format well.
Another example, this time the @
-prefixed blocks (constructs and such)
@foreach (var foo in bar) {
<div>@(foo.A)</div>
<div>@(foo.B)</div>
@foreach (var qux in foo.C) {
<div>@(qux.D)</div>
<div>@(qux.E)</div>
}
}
Looks more or less fine, right? Yea, unfortunately it's actually syntactically invalid. The @
-prefix on the inner foreach
is not permitted, as it occurs within the uninterrupted scope of an outer block (the outer foreach
).
The solution? Omit the @
from the inner foreach
. What happens when I format?
@foreach (var foo in bar) {
<div>@(foo.A)</div>
<div>@(foo.B)</div>
foreach (var qux in foo.C) {
<div>@(qux.D)</div>
<div>@(qux.E)</div>
}
}
Awesome, lost the inner indentation. Poo.
Ok, how about those <text>
blocks, they must be useful for something, right? Sure, wrap the contents of the outer foreach
block in a <text>
block. Now, as far as Razor is concerned, @
-prefixing the inner foreach
is syntactically valid.
@foreach (var foo in bar) {
<text>
<div>@(foo.A)</div>
<div>@(foo.B)</div>
@foreach (var qux in foo.C) {
<div>@(qux.D)</div>
<div>@(qux.E)</div>
}
</text>
}
Ok, fine; however, while this works it does exemplify my original question's issue. Now the <text>
block contents aren't adhering to indentation. Why can't we just have:
@foreach (var foo in bar) {
<text>
<div>@(foo.A)</div>
<div>@(foo.B)</div>
@foreach (var qux in foo.C) {
<div>@(qux.D)</div>
<div>@(qux.E)</div>
}
</text>
}
Or better yet, screw the <text>
block and permit @
-prefixed constructs within the scope of others:
@foreach (var foo in bar) {
<div>@(foo.A)</div>
<div>@(foo.B)</div>
@foreach (var qux in foo.C) {
<div>@(qux.D)</div>
<div>@(qux.E)</div>
}
}
tl;dr
Call me anal, call me insane. Doesn't matter; so far as I can tell, this product is not working correctly, regardless of my formatting settings.
Does anyone know how to force VS 2010 Razor formatting to behave as described? (or, is this just some pipe-dream for my OCD?)
A workaround for it would be:
cshtml
fileXML (Text) Editor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With