Skip to content
  • iImagine
  • Register
  • Log In

Web Development School

Learning made easy.

  • Books
    • Beginning Web Development with ASP.Net Core & Client-Side Technologies
      • TOC
      • Part 1
        • Chapter 1: Static HTML – Designing the landing page
      • Part 2
        • Chapter 2: ASP.Net Core – Let’s talk Dynamic
        • Chapter 3: Introduction to ASP.Net Core MVC
          [ASP.Net Core v9]
      • Part 4
        • Chapter 7: Using Server Side & Client Side technologies together
          [ASP.Net Core v7 & Angular 15]
  • Environment Setup
    • Installing Angular
    • Installing Visual Studio 2022
    • Installing SQL Server 2022 Express
    • Installing Postman
    • Installing Git for Windows
  • Blog
  • iImagine WebSolutions
  • Events
  • Learning Videos
  • Toggle search form

Configure the Application for MVC

In the last module we took a look at the initial files generated by the ASP.Net Core Empty Web project template. Now it’s time to start getting our hands dirty. We need to create a Controller to accept HTTP requests and a View to render data to the user. But first we need to configure the project as an MVC Application. We do that in the Program.cs file we looked at in the last module, the entry point to the application that sets up and runs the application.

Table Of Contents
  1. Modify Program.cs – Configure the application for MVC
    • C# Comments
  2. Adding Services and Creating Endpoints
    • Add the MVC service
    • Add the default Controller Route
  3. What's Next

Modify Program.cs – Configure the application for MVC

Open the Program.cs file by double clicking it in the Solution Explorer inside of Visual Studio.

Modify its contents with the code shown below.

FredsCars/Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add Services
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.

/*** remove initial endpoint ***/
// app.MapGet("/", () => "Hello World!");

/*** Add endpoints for contoller actions and
       the default route ***/
app.MapDefaultControllerRoute();

app.Run();

In the code above we just made a few minor modifications.

C# Comments

Remember from Chapter 1 where we learned about HTML and JavaScript comments? Well, C# has comments too. They look like JavaScript comments.

A one line comment starts with two forward slashes like this:

// My one line comment

Multiple line comments start with a forward slash and Asterix like this /* and end with an Asterix and forward slash like this */. The following is an example of a multiline comment in C#.

/* My mulitline comment.
   Here is another line.
   And one more line.
*/

Adding Services and Creating Endpoints

Notice in particular two comments we added.

/*** ... ***/
// Add Services
/*** ... ***/
// Configure the HTTP request pipeline.
/*** ... ***/

These two comment lines denote the two major sections in Program.cs we use to configure any ASP.Net Core application whether it is an MVC, Razor Pages, or Blazor application or some combination thereof.

  1. Adding Services
  2. Configuring the HTTP request pipeline
    • Register middleware components
    • Create and configure Endpoints.

Add the MVC service

In the Add Services section of Program.cs, we add the services for controllers with the following line.

builder.Services.AddControllersWithViews();

We need to add these services for controllers because the MVC architectural pattern is based on Controllers and Views as opposed to Razor Pages which use Page Models and Views. (We will see Razor Pages in the next chapter)

The AddControllersWithViews method configures the MVC services for the commonly used features with controllers with views.

So far we have only added this single service because it’s the only service we need to get started and run our project as an MVC application. But we will see many more types of services available that we can use in later modules and chapters. And eventually the concept of services will lead us to a software development pattern called DI (Dependency Injection).

Add the default Controller Route

Also notice we “commented out” the MapGet endpoint

/*** remove initial endpoint ***/
// app.MapGet("/", () => "Hello World!");

We did this because we no longer want our base URL (https://localhost:40443) to merely return the string “Hello World!”. We want our endpoints to be handled by the controllers we are going to create. So we call the MapDefaultControllerRoute method of the WebApplication object to add endpoints for controller actions.

app.MapDefaultControllerRoute();

The MapDefaultControllerRoute method adds a default route with the following pattern.

{controller=Home}/{action=Index}/{id?}

This route has three segments which match up with an incoming URL request. The first segment specifies the default controller as the Home controller if none is specified. The second segment specifies the default action as the Index action in the controller if no action is specified. The third segment is an optional route value named id and is marked as optional by the ‘?’ character.

With the above route in place, here are some examples of how some URLs would work.

https://localhost:40443/users/add

The above URL would go to a controller called users and an action method with in the controller called add.

https://localhost:40443/users/index/4

The above URL would go to a controller called users and an action method with in the controller called index and the id route value would be 4.

https://localhost:40443

With no controller or action specified, the above URL would go to the default Home controller and default Index action within the controller. No id value would be passed.

Don’t worry if these examples of the default registered route seem fuzzy for now. It will become more clear once we start writing our own controllers.


But I do want to note an important concept of MVC here. Especially MVC in the context of ASP.Net and ASP.Net Core.

The default route registered by the MapDefaultControllerRoute method is a good example of Convention over Configuration.

Back in chapter 2, module 3, What is ASP.Net, I talked about how when ASP.Net full framework first came out, the only framework available was WebForms, until they added MVC as an alternative several years later.

I showed a little sample of what some WebForms code might look like in that module, but I did not talk about the dreaded web.config file which was the configuration file for WebForms. The XML based configuration file was verbose, ugly, hard to read, and error prone to maintain. So much of the application functionality was hard coded and configured into the XML file which could become a maintenance nightmare (and usually did).

MVC on the other hand uses more convention then configuration. And the URL patterns described above are an example of this.

Any web developer familiar with the MVC pattern will know and understand the URLs described in the examples above and expect that behavior without having to look at a configuration file. But ASP.Net is also very flexible and custamzable. We can make other URL patterns besides the default route and assign endpoints or functionality to them.

And, the few configuration files that ASP.Net Core MVC does have are JSON (JavaScript Object Notation) files. These files are much easier to read and maintaine then XML files and make ASP.Net applications more cloud ready.

What’s Next

In this module we added the services and endpoints needed for an MVC application.

In the next module we will create our first controller called the Home Controller.

< Prev
Next >

Leave a ReplyCancel reply

Chapter 1: Static HTML – Designing the landing page.

  • Static HTML – Designing the landing page.
  • Let’s get started!
  • Mock your site with HTML
  • Make CSS easy with Bootstrap
  • Mock your content
  • Introducing JavaScript
  • JavaScript Code Improvements
  • Results Data
  • Images and the HTML Image Element.
  • Revisiting Reusability for CSS and JavaScript
  • Reuse for HTML: PART 1
  • Reuse for HTML: PART 2
  • Details Page – Using a Bootstrap Component
  • Creating Links
  • Chapter One Conclusion

Chapter 2: ASP.Net Core – Let’s talk Dynamic

  • Introduction to ASP.Net Core
  • What is .Net?
  • What is ASP.Net
  • Introduction to Entity Framework Core

Chapter 3: ASP.Net MVC Core – Models, Views, and Controllers [ASP.Net Core v9]

  • Introduction to ASP.Net Core MVC
  • Create the project: ASP.Net Core MVC
  • Explore the ASP.Net Core Empty Web Project Template
  • Configure the Application for MVC
  • Create a Controller: Home Controller
  • Create a View: Index View for the Home Controller
  • Install Bootstrap using Libman
  • Create the Layout template
  • Create the Model
  • Install EF Core & Create the Database
  • Seed the Database: Loading test data
  • DI (Dependency Injection): Display a List of Vehicles
  • Repository Pattern: The Vehicles Repo
  • Unit Test 1: Home Controller Can Use Vehicle Repository
  • Unit Test 2: Vehicle Repository Can Return List
  • Add the ImagePath Migration and Thumbnail images to results
  • Pagination: Create a Custom Tag Helper
  • Sorting
  • Category Filter
  • Partial View: Break out the vehicle results
  • View Component: Create dynamic category buttons
  • Create the Details page
  • Create the Create Page

Chapter 7: Using Server Side & Client Side technologies together. [ASP.Net Core v7 & Angular v15]

  • Intro to Full Stack Development
  • Fred’s Cars – Full Stack Development
  • Prepare the environment
  • Create the Visual Studio Solution
  • Add the ASP.Net Core Web API project
  • Add the Angular Project
  • Wire it up!
  • WeatherForecast: Understanding the basics
  • Vehicles API Controller: Mock Data
  • Vehicles Angular Component: Consuming Data
  • Routing and Navigation
  • Using a Component Library: Angular Material
  • Our first Angular Material Component: MatToolbar
  • Configuring for Saas: CSS with superpowers
  • Create the Header & Footer components
  • Displaying Results with MatTable
  • Loading: Using a Progress Spinner
  • MatTable: Client-Side Paging and Sorting
  • MatSidenav: Create a Search Sidebar
  • MatCheckbox: Category Search UI
  • Adding an image to the welcome page
  • Create the database with Entity Framework Core migrations
  • MatPaginator & PageEvent: Custom Server-Side Paging
  • Unit Testing: Custom Server-Side Paging
  • Repository Pattern: VehicleRepository
  • Unit Test: Paging in the Vehicles controller
  • Server-Side Sorting
  • Unit Tests: Sorting
  • Filter (Quick Search)
  • Unit Tests: Filter feature
  • Advanced Search: Categories
  • Unit Tests: Search by category
  • Progress Spinner: Final Fix

TOC

  • What were WebForms?
  • Enter MVC
    • Understanding MVC
    • Advantages of MVC
  • ASP.Net Core MVC – A total rewrite
  • ASP.Net Core 2 MVC – Here come Razor Pages
    • Understanding Razor Pages
  • ASP.Net Core 3 – Dropping the MVC reference
    • Understanding Blazor
  • Dropping the MVC reference
  • Hello .Net 5!
  • What’s Next? – Here comes .Net 6.

Recent Posts

  • Angular Commands Cheat Sheet
  • Installing Git for Windows
  • Installing Postman
  • Installing SQL Server 2022 Express
  • Installing Visual Studio 2022

Recent Comments

No comments to show.

Archives

  • November 2023
  • October 2023
  • June 2023
  • October 2021

Categories

  • Angular
  • ASP.Net
  • Environment Setup
  • See All
  • SQL Server
  • Visual Studio
  • Web API & Rest Services

WordPress Theme Editor

Copyright © 2025 Web Development School.

Powered by PressBook Blog WordPress theme