lidgren
lidgren

Reputation: 463

ASP.NET beginner woes with new Web Site

I'm a decently experienced .net developer for the desktop but this is my first stab at using ASP.NET and i'm stumped almost right away.

I've used the VS2008 "New Website" wizard and got a simple page working, codebehind page and all. But my next step of adding a simple static class for some utility methods to be used by all pages is not working out at all.

I've tried putting code in App_Code, I've tried creating a new library project and referencing it... everything always compiles fine in Visual Studio, but when uploaded to the server it simply refuses to find the namespace/class or recognize the library assembly. I've tried messing around with the web.config but haven't been able to find any way to reference the library assembly ("*" doesn't seem to do it for example).

The server pages says: Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074

Is there a best practice way for utility methods and how can I get my codebehind pages to recognize any class (or better yet namespace, or even assembly) besides itself?

The exact error i'm getting is: CS0246: The type or namespace name 'MyCommon' could not be found (are you missing a using directive or an assembly reference?)

Upvotes: 2

Views: 408

Answers (6)

Zoman
Zoman

Reputation: 2107

I would avoid using the Web Site project as it did cause lots of trouble for our team, similar to what you are describing here. Instead, you should use the Web Application project model that is clearer and more suitable for multi-project solutions.

However, if you have to stick with the Web site project model, you should make sure you added a reference of your project (MyCommon) to the web app and then create a using statement in the class you want to use it.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273169

I've seen this error when the class is not in App_Code...

On another note, you could have started with an ASP.NET Application instead of a Website. They are 2 different application models, the Application variant is a little easier with respect ta adding classes and assemblies.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

You are best to ignore the Web Site "projects". They are unique in the Visual Studio world in that they are not projects (no .csproj or other similar file). They will seem very strange and counter-intuitive to any non web site developer.

Instead, use File->Add New Project, and choose "ASP.NET Web Application". That will create a web application project, which will work much more as you expect. You can even add classes in whatever folder you like, and they will work as you'd expect them to work.

Upvotes: 5

JP Alioto
JP Alioto

Reputation: 45117

Make sure you move your project to the server via publishing the web site. Also, you might want to use a web application project (new->project->web->web application project) instead of a new web site. It will probably behave more like you expect it to.

Upvotes: 0

Tone
Tone

Reputation: 2853

Make sure your helper class is in the same namespace as the webform - or fully qualify the particular helper method you are calling. Can you provide some code of what you're trying to do?

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062502

I'm assuming you do have a using statement that points to the namespace?

Something I see from time to time... on the server, have you (in IIS) marked the new folder as an "application"? It should have a cog in IIS. If you haven't, then it'll be looking in the wrong location for App_Code (the "application" tells it where the local app root is). Right-click, properties, "create application" (or something like that - I don't have a server to hand...)

Upvotes: 2

Related Questions