Getting Started with ASP.NET Core MVC: Creating and Configuring Endpoints

Photo by Ben Hershey on Unsplash

Getting Started with ASP.NET Core MVC: Creating and Configuring Endpoints

Adding Endpoints

In this guide, we'll walk through creating an ASP.NET Core MVC application, configuring endpoints, and setting up a new controller and view.

Step 1: Create an ASP.NET Core MVC Application

First, create a new ASP.NET Core MVC application. You can do this using Visual Studio or the .NET CLI.

Using Visual Studio:

  1. Open Visual Studio and select Create a new project.

  2. Choose ASP.NET Core Web App (Model-View-Controller) and click Next.

  3. Configure your new project and click Create.

  4. Select the latest .NET version and click Create again.

     dotnet new mvc -n MyMvcApp
     cd MyMvcApp
    

    Step 2: Configure Endpoints in Startup.cs

    In the Startup.cs file, locate the Configure method. Here, you'll find a middleware component called UseEndpoints. This middleware is responsible for defining the endpoints that your application will respond to.

    By default, an endpoint for the Home controller will be predefined.

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
     {
         if (env.IsDevelopment())
         {
             app.UseDeveloperExceptionPage();
         }
         else
         {
             app.UseExceptionHandler("/Home/Error");
             app.UseHsts();
         }
    
         app.UseHttpsRedirection();
         app.UseStaticFiles();
    
         app.UseRouting();
    
         app.UseAuthorization();
    
         app.UseEndpoints(endpoints =>
         {
             endpoints.MapControllerRoute(
                 name: "default",
                 pattern: "{controller=Home}/{action=Index}/{id?}");
         });
     }
    

    Step 3: Add a New Controller

    Now, let's add a new controller named TestController.

    1. Right-click on the Controllers folder in Solution Explorer.

    2. Select Add > Controller.

    3. Choose MVC Controller - Empty and click Add.

    4. Name your controller TestController.

       using Microsoft.AspNetCore.Mvc;
      
       namespace MyMvcApp.Controllers
       {
           public class TestController : Controller
           {
               public IActionResult Index()
               {
                   return View();
               }
           }
       }
      

      Step 4: Create a View for the Test Controller

      Next, we need to create a view of the TestController.

      1. Right-click on the Views folder in Solution Explorer.

      2. Add a new folder named Test.

      3. Right-click on the Test folder, then select Add > New Item.

      4. Choose Razor View and name it Index.cshtml.

         @{
             ViewData["Title"] = "Test Page";
         }
        
         <h2>Welcome to the Test Page!</h2>
         <p>This is a simple view for the Test controller.</p>
        

        Step 5: Run the Application

        Run the application by pressing F5 or clicking the Run button in Visual Studio. Alternatively, use the .NET CLI:

         dotnet run
        

        Navigate to https://localhost:5001/Test (or the corresponding URL provided by your setup) to see the TestController in action.

        Feel free to expand this application by adding more controllers, views, and custom routes to suit your needs.

        Thank you for following along!

        Harsh Chandwani

        Software Dev