views:

227

answers:

5

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?)

+2  A: 

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

Marc Gravell
On Vista (IIS7) there is a convert to application option that should do the trick.
RichardOD
Yes, I've tried the global namespace and explicit namespaces; they all match up nicely in visual studio and compile file, but breaks on the server. Unfortunately I only have FTP access to the server, so I can't check icons...
lidgren
Without configuring the folder in IIS as an application, it isn't going to work... what server is it? IIS 6? IIS 7?
Marc Gravell
I don't know exactly; it's a web hotel. But; the simple case (just a Default.aspx with a codebehind page) worked just fine... would that work without marking the folder as an application? Perhaps only the root folder is marked... i'll try some more.
lidgren
This is definitely looking like the right thing to investigate to me.
RichardOD
AHA! This was indeed the problem. The root folder is probably marked as an application, but I tried to publish the Web Application to a subfolder; I switched to just publishing to root and it seems to work - thanks Marc (and everyone)!
lidgren
A: 

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?

Tone
A: 

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.

JP Alioto
I've actually tried that too and had similar problems, so I went back to web site (ctrl-s to "publish" seemed easier...)
lidgren
+4  A: 

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.

John Saunders
I tried it briefly but failed with similar problems; but since it seems the general consensus is that Web Application is better I'll probably have another go at it.
lidgren
This doesn't help solve the problem but I do agree with you John that apps are better than sites. There's a blog post on this here- http://mikehadlow.blogspot.com/2009/04/sorry-tale-of-visual-studio-web-site.html
RichardOD
@lidgren: I suggest you try with web application project, then if you have trouble, create a new question. Your problem may not actually be the same as this one.
John Saunders
A: 

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.