Regions In ASP.NET Views?
Solution 1
This works in Visual Studio 2015 and above
(thanks to @dotnetN00b
for the sample in the comments section):
<!-- #region Test -->
code here
<!-- #endregion -->
Solution 2
Select the part which needs to be converted to region, then right click and press CollapseTag
Solution 3
In Visual Studio (2015 and above) .html or .cshtml code editor, type region
, then press Tab
key.This implements #region
snippet code like below:
<!-- #region name -->
//Your html or cshtml codes
<!-- #endregion -->.
Solution 4
In Visual Studio, you can manually add outlined region like this :
To create or remove a collapsible region
Select the text you want to treat as a collapsible region.
To create a collapsible region, on the Edit menu, point to Outlining, and then click Hide Selection.
The editor turns the selection into a region, collapses it, and displays a box with an ellipsis (...) to indicate that the area contains a collapsed area. You can hold the mouse pointer over the box to see its contents.
To remove a collapsible region, collapse it, and then click it to select it.
On the Edit menu, point to Outlining, and then click Stop Hiding Current.
To collapse and expand a single region
To collapse a region, click the minus sign (-) in the margin of the editor.
To expand a collapsed region, click the plus sign (+) in the margin.
To collapse and expand all regions
On the Edit menu, point to Outlining, and then click Toggle All Outlining.
But that's not really practical.
For HTML you can manually edit the outline option for each tags in the text editors options :
Minimum value of minimum lines is 1 to be effective.
Solution 5
I don't have "CollapseTag" option in my context menu. What I usually do is :
- Select text.
- Goto Edit -> Outlining -> Hide Selection.
or
use Ctrl+M, Ctrl+H
I am using Microsoft Visual Studio Pro 2013.
Velter
Updated on November 07, 2021Comments
-
Velter over 2 years
I am making an ASP.NET MVC application with the razor engine.
And I was wondering if it's possible to use Regions in a view.something like:
#region blabla <p>@Model.Name</p> <p>...</p> #endregion
This does not work. Is there an alternative?
-
Dan over 12 yearsExactly what I needed! Thanks SO much!
-
Dangerous about 12 yearsI never knew that you could do this although I can't see how to give the region a name. That said, I can't see any reason why this should not be marked as the correct answer?
-
Jonathan about 12 yearsIn fact, this is the correct answer. This answer has been awarded bounty by Chuck Norris, so No matters where the green tick is. This is the correct one.
-
Myster over 11 yearssuggest the answer starts: No, you can't use regions, but you could try this instead
-
Joel Peltonen about 11 yearsNote that this is not saved when your close and reopen the file in question, which makes them not really interesting at least in my case.
-
Ed DeGagne over 10 yearsUseless, as they do not save when closed.
-
JoeBrockhaus about 10 yearsAlso note this won't actually allow you to collapse a group of elements, but their parent and/or individual elements, which won't give the desired effect in this case. In my case, I'm looking for collapsable
#region
elements within a code block. Since I'm using asp.net in MVC, I put the region demark and then use close/end server tags. It's ugly when expanded but it works:<% // non-collapsed code .. %><% #region collapsable code #endregion %><% // resume non-collapsed code.. %>
OR you can use Ctrl-M + Ctrl-H & Ctrl-M + Ctrl-U (stackoverflow.com/questions/6457967) -
dotnetN00b almost 10 yearsIf you have VS 2012, then download Web Essentials 2012. In either case, you'll need to go to your menu in VS (2012 for me), Tools > Extensions and Updates > Online and search for the tool. In short, IMO, this is definitely the answer until MS provides built-in support.
-
dotnetN00b almost 10 yearsThe syntax is as simple as
<!-- #region Test -->
and<!-- #endregion -->
. -
Arturo Torres Sánchez over 9 yearsThese are not regions at all, just collapsing tags. Even Chuck Norris can be wrong sometimes.
-
ProfK over 9 yearsPoints for the Outlining Collapse. I find it very practical.
-
Catto over 8 yearsWorks with VS2015 with no plugin - nice & perfect answer
-
Excel Kobayashi over 8 yearsThis is the only correct answer here, the others are talking about collapsing the PARENT TAG, which is not the same thing
-
Luke Vo over 8 yearsThis should be the correct answer! Works with VS2015 without any plugin.
-
Mr. Robot over 8 yearsThis is perfect (y) I love it. Thx so much
-
Jon Story almost 8 yearsDefinitely the better option in recent VS versions - and more natural
-
Velter over 7 yearsI changed the correct answer to this one! Thanks for all the input :)
-
Kjell Rilbe over 6 yearsJust tried it in VS2017, but doesn't seem to work without plugin (as reported for VS2015). Anyone know the status for VS2017?
-
Kjell Rilbe over 6 yearsCorrection: It works as in regular C# code
#region Test
...#endregion
but only in C# code secions within the Razor view file. If any HTML mode section appears inside the region, it won't work. -
Arijit Mukherjee over 6 yearscan we use region inside another region?
-
Morten Jensen about 6 yearsWorks for me in VS2013
-
Juan Salvador Portugal almost 6 yearsI dont have the CollapseTag option when i rightclick in VS2017, someone know how can turn it on?
-
wesreitz almost 3 yearsWorks for me in Visual Studio 2019 Professional
-
Muhammad Ali Yousaf over 2 yearsIts not working on Visual Studio for Mac, version is 8.10.10 (build 8). Do I need to install any plugin?