Wednesday, August 25, 2010

WebMatrix RendorPage() and Functions: Not Quite Include Files

Out of curiosity, I added a function to a header.cshtml file like so-

@functions {
   int AddTEST(int a, int b)
   {
      return a + b;
   }
}


Then I tried calling the function in index.cshtml like so-

@AddTEST(1,2);

As I thought, if failed. So, just to keep in mind all you scripters that RenderPage() in WebMatrix is not quiet like include files in PHP or Classic ASP. For functionality that is shared across pages you will need to create a class for that, which is pretty easy to do, but not quiet what you might be used to. Welcome to OOP :)

3 comments:

Anonymous said...

Not really, you just forgot to put "public static" in front of "int add."

I just did this function in webmatrix, worked a treat, then i made a GetValue DB function, pass in fieldname, tablename, whereclause, awesome reuse.

Anonymous said...

Also, forgot to say, need to put this code into App_Code folder.

Patrick
CoffeeRunnr.com

infocyde said...

Late response, but thanks for the info.