Welcome to WebmasterWorld Guest from 100.26.182.28

Forum Moderators: ocean10000

Message Too Old, No Replies

Global Functions File for ASP.NET

     
7:49 pm on Jun 2, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0


Another newbie question incoming! I am in the prep stages for an upcoming site, which will probably be my most ambitious ASP.NET site so far (I'm usually a PHP guy).

I have some functions that I know I will need to use all across the website...things to access the DB a certain way, etc. I cannot find a good, simple example of how to create and implement a file where I can store global functions.

I've found info on doing it within Visual Studio, but I'm not using VS, I'm using dreamweaver.

Can anyone help me out with some kind of basic example of how to implement global functions for my site? Also how to call them?

Thanks!

Chris
8:00 pm on June 2, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:733
votes: 0


It's fairly easy once you know how :)

Create a new class in the App_Code folder, if there are only a few functions I just call it Helpers.cs.

Then create a static method, ie:

public static string SayHello(string name)
{
return "Hello " + name;
}


You should now be able to call this method from anywhere within your project:

Helpers.SayHello("Chris");


This is just off the top of my head, and untested, but I hope you get the general idea.
8:13 pm on June 2, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts:186
votes: 0


Marcel, you are always so quick and helpful! Thanks I will give this a try in the next few days.
2:25 pm on June 3, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0


I'm still having a problem. Before I go too far let me make sure that this wouldn't cause any issue. There is a live site running in IIS (www.example.com). I'm working on a complete reworking of that site and I'm doing that at www.example.com/2010. Should their be any problem with me having an App_Code folder under the 2010 folder (www.example.com/2010/App_Code) and that App_Code folder should only apply to what is in the 2010 folder? Hope that makes sense.

I was getting errors on the page so I broke down and for the sake of understanding fired up Visual Web Dev Express 2005. Below is my code. In VWD2005 it compiles and shows the file under local host, but if I try it from www.example.com/2010/ I get this error:

The name 'Helpers' does not exist in the current context

Below is the code.

Helpers.cs
using System;
using System.Data;
using System.Web;

public class Helpers
{
public static string SayHello(string name)
{
return "Hello " + name;
}
}


default.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
Helpers.SayHello("Chris");
}

}


Any thoughts?

[edited by: marcel at 9:43 am (utc) on Jun 4, 2010]

2:31 pm on June 3, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts:186
votes: 0


OK...nevermind...as is sometimes the case I answer my own question moments I after I post (which came after a long time of searching). I put the Helpers.cs in the ROOT App_Code folder (www.example.com/App_Code) and it works fine. Thanks!

[edited by: marcel at 9:44 am (utc) on Jun 4, 2010]
[edit reason] examplified [/edit]

9:55 am on June 4, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:733
votes: 0


One thing you need to look out for in this case is that you could create a naming conflict if there is for example already a Helpers Class in the Bin folder of the root.

One option is to convert the www.example.com/2010/ subfolder to an application in IIS, then it is possible to use the www.example.com/2010/App_Code folder. But, you will no longer be able to make use of for example the web.config from the root foler.
1:04 pm on June 4, 2010 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts:186
votes: 0


Thanks Marcel...I find it interesting that I can put a web.config at www.example.com/2010 and it overrules the one at www.example.com. For example the root site is set to show a graceful error page, but on the 2010 folder I need to see errors as I'm working so I'm able to create a web.config there that shows errors. Interesting that I can do that but not another App_Code folder. But I kind of get the fact that one is just a set of directives and the other is compiled into the project.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members