Creating Your First ASP.NET MVC App Using C# - A Step-by-Step Guide

Category > CSHARP || Published on : Friday, March 17, 2023 || Views: 686 || ASP.NET MVC C# MVC pattern Model-View-Controller Web development


In this article, we will guide you through the process of creating your first ASP.NET MVC app using C#. We will cover the basics of creating a new MVC project, understanding the project structure, creating a controller, creating a view, adding a model, passing the model to the view, and displaying the data in the view. By following these steps, you can quickly create a functioning app using the MVC pattern.

Creating a First MVC App in ASP.NET MVC

ASP.NET MVC is a framework for building web applications using the Model-View-Controller (MVC) architectural pattern. It is one of the most popular web development frameworks, and it is widely used by developers around the world. In this article, we will go through the steps of creating your first MVC app in ASP.NET MVC using C#.

Step 1: Create a new ASP.NET MVC project

To create a new ASP.NET MVC project, you need to have Visual Studio installed on your computer. Open Visual Studio and click on "Create a new project." In the "New Project" dialog, select "ASP.NET Web Application" and click "Next." In the next screen, select "MVC" as the project template and click "Create."

Step 2: Understanding the Project Structure

When you create a new ASP.NET MVC project, Visual Studio creates a project structure that contains various files and folders. Here is a brief explanation of the important files and folders:

  • App_Data: This folder is used to store data files.

  • App_Start: This folder contains configuration files and code that runs when the application starts.

  • Controllers: This folder contains the controllers of the application.

  • Models: This folder contains the models of the application.

  • Views: This folder contains the views of the application.

  • Scripts: This folder contains JavaScript files used in the application.

  • Content: This folder contains CSS files and images used in the application.

Step 3: Create a Controller

The controller is responsible for handling incoming requests and generating responses. To create a controller, right-click on the "Controllers" folder and select "Add" > "Controller." In the "Add Scaffold" dialog, select "MVC Controller - Empty" and click "Add."

In the "Add Controller" dialog, enter the name of the controller, such as "HomeController," and click "Add." This will create a new empty controller with the specified name.

Step 4: Create a View

The view is responsible for displaying data to the user. To create a view, right-click on the "Views" folder and select "Add" > "View." In the "Add View" dialog, enter the name of the view, such as "Index," and select "Create a strongly-typed view" if you want to use a model with the view. Click "Add" to create the view.

Step 5: Write Code in Controller

Now that you have created a controller and a view, you can write code to display data in the view. In the HomeController, add an action method called "Index" that returns the view. Here's an example:

public class HomeController : Controller
{
   public ActionResult Index()
   {
      return View();
   }
}

In this code, we have added an action method called "Index" that returns the view. The View() method without any parameters returns the default view for the action method, which in this case is the "Index" view we created in Step 4.

Step 6: Run the Application

Now that you have written the code, you can run the application to see the results. To run the application, click on the "Start" button in Visual Studio. This will launch the application in your default browser.

If everything is working properly, you should see the "Index" view displayed in the browser.

Step 7: Add a Model

In the Person class, we'll add two properties - Name and Age - as shown below:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Step 8: Pass Model to View

Now that we have created a model, we can pass it to the view. In the HomeController, modify the Index action method to create a new instance of the Person class and pass it to the view:

public class HomeController : Controller
{
   public ActionResult Index()
   {
      var person = new Person { Name = "John Doe", Age = 30 };
      return View(person);
   }
}

In this code, we have created a new instance of the Person class and set its Name and Age properties. We then pass this instance to the View method as a parameter.

Step 9: Display Model Data in View

In the Index view, we can display the data of the Person model using Razor syntax. Replace the content of the Index view with the following code:

@model Person

<h2>Welcome, @Model.Name!</h2>
<p>You are @Model.Age years old.</p>

In this code, we are using the @model directive to specify the type of the model that the view expects. We are then displaying the Name and Age properties of the Person model using Razor syntax.

Step 10: Run the Application

Run the application again, and you should see the name and age displayed in the browser.

Congratulations! You have successfully created your first ASP.NET MVC app using C#. We covered the basic steps of creating a new MVC project, understanding the project structure, creating a controller, creating a view, adding a model, passing the model to the view, and displaying the data in the view. With this foundation, you can now explore and learn more about the ASP.NET MVC framework.

In summary, creating an ASP.NET MVC app involves creating a project, understanding the project structure, creating a controller, creating a view, adding a model, passing the model to the view, and displaying the data in the view. By following these steps, you can quickly create a functioning app using the MVC pattern.