Last post Apr 11, 2014 08:42 AM by CASPartan
Apr 11, 2014 08:18 AM|CASPartan|LINK
Is classic asp, I have my own library of common functions.
Then on top of each .asp file, I include them.
I want to do the same sort of thing with ASP.NET. Googling around, I got this working. But don't know if this is the RIGHT way to do this sort of thing. Looking for guidance and suggestions.
Here's what I did:
under /App_Code, I made a CommonClass.cs file
/// Summary description for CommonClass
public class CommonClass
public String SayHello(string strName)
var msg = "Hello there " + strName + "!" ;
public String SayGoodbye(string strName)
var msg = "goodbye there " + strName + "!";
then on each .cshtml page, I added this.
var obj = new CommonClass();
I can then call the above functions like so
My question is: Is this the right way to implement site-wide common functions?
Or is there a better, easier way? Thanks.
Apr 11, 2014 08:32 AM|Mikesdotnetting|LINK
If the methods/functions are just utility functions, you can make the methods static which means you do not need to instantiate an instance of the CommonClass. You can just call the methods:
Otherwise your approach is fine.
There are other approaches depending on the nature of the reusable code. I wrote an article recently about this: http://www.mikesdotnetting.com/Article/227/Migrating-Classic-ASP-To-ASP.NET-Razor-Web-Pages-Part-Three--Include-Files-And-Reusable-Code.
It's in VB, but you should get the drift.
Apr 11, 2014 08:42 AM|CASPartan|LINK
Awesome. Got it working like you said. Easier and less typing too.
I will check out your article.