Groups | Blog | Home
all groups > asp.net > january 2005 >

asp.net : accessing user defined utility functions


nzanella NO[at]SPAM gmail.com
1/17/2005 11:16:14 PM
Hello,

I need to access some user defined utility functions from within
my ASP.NET pages. I wonder whether there is a way to do this. I
do not want to use inheritance. I just want to be able to call
some code contained in a .cs file (C# file) from within several
..aspx and .ascx page without having to rewrite the code in each
such page. I would like to know how this can be accomplished,
including how I can ensure that ASP.NET will find the C#
source file.

Thanks,

Chinmay
1/17/2005 11:47:02 PM
ASP.Net won't go and look for .cs files.... when you compile your project
you'll get a DLL and that DLL will be used for all the functinalities not the
..cs. Morover, you can make a Custom Class in your project and add the code
you want to call in every page. than create objects of that class and you are
William F. Robertson, Jr.
1/18/2005 10:17:49 AM
You will need to add a seperate code file for this code.

Example:

Add a class file "StringUtils.cs".

//sealed keeps anything from deriving from this
public sealed class StringUtils
{
public static string FormatMoney( decimal amount )
{
//return custom formatting of the amount
}
//private keeps the class from being instantiated.
private StringUtils() { }
}

Now in all your pages in the project you can access this

lblAmount.Text = StringUtils.FormatMoney( amount );

HTH,

bill

[quoted text, click to view]

MWells
1/18/2005 9:16:00 PM
By user-defined, I'm guessing you mean that your website user provides some
C# source to work with. There actually is a way to compile cs at runtime;
though it's not an ASP.NET specific feature.

You'll probably want to provide your users with some kind of
code-upload-and-maintenance UIs, and store the content in a database. One
way or another, you need to get the cs code to your webserver in order to
run it.

For on-the-fly source compilation and execution, google;

Microsoft.CSharp.CSharpCodeProvider

Also see...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfmicrosoftcsharpcsharpcodeproviderclasstopic.asp

http://www.codeproject.com/csharp/RuntimeCompiling.asp

/// M

[quoted text, click to view]

nzanella NO[at]SPAM cs.mun.ca
1/19/2005 5:31:45 PM
Thank you for your reply,

I have tried your suggestion and received the following
compilation error, which makes me think there is
something else I should specify:

Compiler Error Message: CS0246: The type or namespace name
'StringUtils' could not be found (are you missing a using directive or
an assembly reference?)

Please note that I am not using Visual Studio and am hand
coding the files by hand. I would be very thankful if someone
please let me know what I should do to get rid of this
compilation error.

Thanks,

Neil
nzanella NO[at]SPAM cs.mun.ca
1/22/2005 4:24:03 PM

Here is how I solved the problem:
<%@ Assembly Src="foo.inc" %>

Regards,

Neil
AddThis Social Bookmark Button