Confusion about Namespaces and App_Code, etc. in VS2008/ASP.net3.5


A

Alex Maghen

I'm not sure whether this is about Visual Studio 2008 or ASP.NET 3.5 but
something has changed that I just don't understand:

It used to be that when I created a class and placed it in the special
App_Code directory, I could wrap it in any namespace I liked and then use the
"using" directive in my .aspx pages for that Namespace and I'd have access to
the class.

Now, when I create a class, it is automatically given a crazy namespace that
is something like <projname>.<foldername>. And yet, if I reference that
namespace from .aspx.cs pages, I still cannot access the class.

What has changed here?

Alex
 
Ad

Advertisements

A

Allen Chen [MSFT]

Hi Alex,

From your description I think you're using Web Application instead of Web
Site project. Please try to create a Web Site project to test if the
problem still occurs.

In a Web Application project the App_Code folder is just like a normal
folder. The classes in the Web Application project is intended to be
compiled to an assembly at compile time instead of being automatically
compiled at run time. The problem you met with is due to that the IDE
doesn't know the file in the App_Code folder need to be compiled. To add
class file in App_Code folder (or any other folder you like) I recommend
you right click the project name to add a new item instead of right
clicking the App_Code folder icon. After that, drag the class file to the
App_Code folder. In this way the IDE will compile the class file.

If you right click the App_Code folder to create the class file you can
notify the IDE in this way:

1. Find the class file in the App_Code folder. Select it.

2. In the properties window change the Build Action from "Content" to
"Compile".

3. Now try to access the class in your aspx.cs. It should work.

Or manually edit the *.csproj file:

1. Open the *.csproj file of the current project with Notepad.

2. Find this item in it (assume you've created the Class4.cs in the
App_Code folder by right clicking the folder icon in the solution explorer
window to add new item):

<Content Include="App_Code\Class4.cs" />

3. Replace it with:
<Compile Include="App_Code\Class4.cs" />

4. Save the file.
5. Back to Visual Studio, click "Reload" when you see a popup window.
6. Now try to access the Class4 in your aspx.cs. It should work.

Please have a try and feel free to ask if you have further questions.

Regards,
Allen Chen
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements

A

Allen Chen [MSFT]

Hi Alex,

Have you got the expected answer? Please feel free to ask if you have
further questions.

Regards,
Allen Chen
Microsoft Online Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top