DEV Community

KateYoung
KateYoung

Posted on

How to add class file to web application in ASP.NET

IDE
Visual Studio 2019

Scenario
I was trying to create a public class and add it to a webform.

Solution
After researched a bunch of materials, I finally figured the simple way to do it.

In Class file

  1. Create an ASP.NET folder"App_Code" in your project.
  2. Create a class file in the folder "App_Code".
  3. Declared the class as a public class and type your methods there.
  4. Right-click the class file to properties
  5. Look for "Build Action", change it to "Compile"

In webform

  1. Go back to the webform that you wanted to add a class file and type "using webForms.App_Code;" at the top of the page.
  2. There you go! Now you can reference your public class file anywhere you want!

Top comments (0)