Visual Studio ASP.Net expand and collapse issue in ashx generic handlers
Asked Answered
L

5

15

I have Visual Studio 2008 Professional and I am having issues with expanding and collapsing method code blocks in ASP.Net Generic Handler pages (.ashx)

I would have thought you could do the same thing like in the code behind of .aspx web pages.

I have this same issue on other boxes even with VS 2008 Standard and VS 2005 Professional. All boxes have been fully patched (OS and Visual Studio.)

Does anybody have any suggestions as to enabling this feature?

Lifesaving answered 11/12, 2008 at 14:29 Comment(1)
Great question. I hope someone else can come up with a better answer, though. My annoyance is that when editing ASHX files, the code indentation preferences for HTML are used instead of the C# settings.Marcellusmarcelo
A
23

You can force Visual Studio to ignore the fact that it's code in front you're working with by going to:

Tools | Options

And opening the "Text Editor | File Extensions" tab.

Create a new entry for extension "ashx", mapped to editor "Microsoft Visual C#" (or "Microsoft Visual Basic", as your preference takes you), and "Add" it.

OK the dialog, close and re-open your ashx file, and your code blocks willl collapse to your hearts content, but the @ directive will be rather ugly.

You have the same issue if you have serverside script in the .aspx file (for example in a web site project and you don't "Place code in a seperate file"), then you cannot collapse the class blocks in there either.

Articular answered 11/12, 2008 at 15:57 Comment(10)
And afterwards, to stop visual studio's whining about the WebHandler directive, just comment out the line, it will work as if it's not!Justus
this absolutely fixes the outlining, but unfortunately breaks intellisenseCursive
It fixed intellisense at my machine (vs2012)Rie
this will cause your code to be like incorrect, vs will higlight as red a lot of your liines of code. UselessCosby
@Cosby I assume you're not still using VS 2008? I'd recommend using bigEsmurf's suggestion to move to a code-behind solution and have full integration that way.Articular
I'm, using vs2015Cosby
I'd still suggest bigEsmurf's solution now.Articular
This breaks intellisense and colorizer.Postrider
@Perry, yep, that was flagged by MaxPRafferty some 11 years ago ;) Personally I'd strongly recommend moving to the more usable Code Behind recommendation that bigEsmurf proposed in their answer. To be honest you probably only need the App_Code directory if you're still deploying Web Site projects, you should also be able to use properly namespaced class reference.Articular
@Zhaph - Ben Duguid, Yes I saw bigEsmurf's answer but the problem with putting all pages code in App_Code is they get compiled as a monolith so every time you change the smallest thing in a page the whole App_Code gets auto recompiled taking a long time on large project. I think using the CodeBehind attribute of <%@ WebHandler %> to split the code in separate file while still outside the App_Code folder might be a better solution although I don't like the extra files for every page which his solution would have too.Postrider
P
12

Create a class in the App_Code directory, which the ashx-file just references... like this:

SomethingHandler.ashx:

<%@ WebHandler Language="C#" Class="SomethingHandler" %>

And in the App_Code folder I've created the file SomethingHandler.cs with class SomethingHandler

using System;
using System.Web;
// using blabla...

public class SomethingHandler : IHttpHandler
{
        public void ProcessRequest(HttpContext c)
        {
    etc...

Now I can just open SomethingHandler.cs, edit my C# code with #region collapsing, because the .cs file is opened in the right editor :)

@ WebHandler docs

Tested in VS 2019.

Postpone answered 21/4, 2011 at 7:53 Comment(3)
That's a very neat little solution :)Articular
AWESOME solution <3Smacker
This also allows us to start using Refactor.Rename in handler code!Sumption
S
8

Just select a fragment of code, like:

using System;
using System.Web;
using System.Web.Security;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

Then Press "Ctrl+M+H" and Vualá... The Outlining Working Now... And Intellisense Too...

To Stop Outlining Press "Ctrl+M+P"...

Stillage answered 30/6, 2015 at 16:58 Comment(2)
This is the most practice solution that i found.Monovalent
You are a rockstar. Ty.Gridiron
C
6

Add /// in front of first line.

Like this:

///<%@ WebHandler Language="C#" Class="FooBar"%>
Canner answered 26/7, 2011 at 11:7 Comment(1)
This only will work if you add extension "ashx" mapped to editor "Microsoft Visual C#" at "Tools/Options/Text Editor/File Extensions" tab.Monovalent
H
0

The easiest fix that won't interfere with IntelliSense or require you to edit the first line is to disable 'smart' indenting and set it to 'block' instead:

Tools -> Options in Visual Studio

Heterophyte answered 27/3 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.