Regions In ASP.NET Views?

46,330

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

  1. Select the text you want to treat as a collapsible region.

  2. 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.

  1. To remove a collapsible region, collapse it, and then click it to select it.

  2. On the Edit menu, point to Outlining, and then click Stop Hiding Current.

To collapse and expand a single region

  1. To collapse a region, click the minus sign (-) in the margin of the editor.

  2. 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.

From MSDN

But that's not really practical.

For HTML you can manually edit the outline option for each tags in the text editors options :

enter image description here

enter image description here

Minimum value of minimum lines is 1 to be effective.

More info on MSDN

Solution 5

I don't have "CollapseTag" option in my context menu. What I usually do is :

  1. Select text.
  2. Goto Edit -> Outlining -> Hide Selection.

or

use Ctrl+M, Ctrl+H

I am using Microsoft Visual Studio Pro 2013.

Share:
46,330
Velter
Author by

Velter

Updated on November 07, 2021

Comments

  • Velter
    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
    Dan over 12 years
    Exactly what I needed! Thanks SO much!
  • Dangerous
    Dangerous about 12 years
    I 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
    Jonathan about 12 years
    In 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
    Myster over 11 years
    suggest the answer starts: No, you can't use regions, but you could try this instead
  • Joel Peltonen
    Joel Peltonen about 11 years
    Note 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
    Ed DeGagne over 10 years
    Useless, as they do not save when closed.
  • JoeBrockhaus
    JoeBrockhaus about 10 years
    Also 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
    dotnetN00b almost 10 years
    If 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
    dotnetN00b almost 10 years
    The syntax is as simple as <!-- #region Test --> and <!-- #endregion -->.
  • Arturo Torres Sánchez
    Arturo Torres Sánchez over 9 years
    These are not regions at all, just collapsing tags. Even Chuck Norris can be wrong sometimes.
  • ProfK
    ProfK over 9 years
    Points for the Outlining Collapse. I find it very practical.
  • Catto
    Catto over 8 years
    Works with VS2015 with no plugin - nice & perfect answer
  • Excel Kobayashi
    Excel Kobayashi over 8 years
    This is the only correct answer here, the others are talking about collapsing the PARENT TAG, which is not the same thing
  • Luke Vo
    Luke Vo over 8 years
    This should be the correct answer! Works with VS2015 without any plugin.
  • Mr. Robot
    Mr. Robot over 8 years
    This is perfect (y) I love it. Thx so much
  • Jon Story
    Jon Story almost 8 years
    Definitely the better option in recent VS versions - and more natural
  • Velter
    Velter over 7 years
    I changed the correct answer to this one! Thanks for all the input :)
  • Kjell Rilbe
    Kjell Rilbe over 6 years
    Just tried it in VS2017, but doesn't seem to work without plugin (as reported for VS2015). Anyone know the status for VS2017?
  • Kjell Rilbe
    Kjell Rilbe over 6 years
    Correction: 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
    Arijit Mukherjee over 6 years
    can we use region inside another region?
  • Morten Jensen
    Morten Jensen about 6 years
    Works for me in VS2013
  • Juan Salvador Portugal
    Juan Salvador Portugal almost 6 years
    I dont have the CollapseTag option when i rightclick in VS2017, someone know how can turn it on?
  • wesreitz
    wesreitz almost 3 years
    Works for me in Visual Studio 2019 Professional
  • Muhammad Ali Yousaf
    Muhammad Ali Yousaf over 2 years
    Its not working on Visual Studio for Mac, version is 8.10.10 (build 8). Do I need to install any plugin?