razorwebmatrix-2

How to call a class method in WebMatrix (oop)?


I have a class called "Cars.cs" and I need to access the method of the class. I'm doing the following: (razor WebMatrix project in c #)

   using System;
   using System.Collections.Generic;
   using System.Web;

   public class Cars
   {
      public string cars {get;set;}
      public int number {get;set;}

   public Cars()
   {

   }

   public List<Car> GetCars()
   {
    List<Car> list = new List<Car>
    {
      new Car{cars="Ford",number=432},
      new Car{cars="Fiat",number=798}  
    };
    return list;
   }

}

Default.cshtml

@{
     Cars c = new Cars();
     c.GetCars();
 }

but I have the following problem: Compiler Error Message: CS0246: Can not find the type or namespace name 'Cars' (are you missing a using directive or an assembly reference?)

I tried putting @ {using cars;} and it is not.

grateful if you can help me

thank you!


Solution

  • Create a folder called App_Code in the root of your site if you don't already have one, and put Cars.cs in that.

    Web Pages sites are "Web Site" projects, so any source code added to App_Code will be compiled at runtime and will be available to all other pages in the application. You can read more about App_Code here: Shared Code Folders in ASP.NET Web Site Projects