Server Technology - Week 1: Profielpagina

Week 1: Profielpagina

Agenda

  • Quiz: Review of previous lesson (15 min)
  • Introduction to framework fundamentals and theory: controllers and views (15 min)
  • Assignment + discussion: controllers and views (20 min)
  • Theory: routing (10 min)
  • Break (10 min)
  • Assignment + discussion: routing (20 min)
  • Theory: middleware (10 min)
  • Assignment + discussion: middleware (15 min)
  • Classroom conclusion: homework and next lesson (5 min)

Profiel Pagina Requirements

  • As a user, I want to be able to view the developer's profile.
  • Refer to the requirements analysis, functional design, and technical design for more information.

Quiz

  • Test on Brightspace under week 1, les 2.

Lesson Objectives

  • Understand how controllers and views work together.
  • Understand what routing is and how it works in ASP.NET Core.
  • Understand the concept of middleware and how it can be used within a framework.

Framework Fundamentals

  • Frameworks are essential:
    • Solve many problems.
      • Controllers and views: splitting code.
      • Routing: executing the correct code for a specific request.
      • Middleware: easily add different layers of functionality to the application, such as authentication.
  • Key concepts covered:
    • Dependency Injection
    • Controllers and views
    • Routing
    • Middleware

Controllers and Views

  • Controller: Responds to events, usually resulting from user actions.
  • View: What the user sees, containing what to show the user and how to show it (HTML).
HomeController Example
public class HomeController: Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController (ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        return View();
    }
}

Controllers and Views - Assignment

  • Use the provided ControllersEnViews.zip file.
  • In the Index and Privacy methods of the HomeController, observe the return View() statements.
    1. Examine the code.
    2. Start the application and observe the results.
    3. What happens when you click on ‘Privacy’? (Hint: Check the URL)

How Framework Determines Which View to render

  • The framework determines which view (HTML) to render based on the controller action.

Routing

  • View (visual representation of data) - UI of the application, the (HTML) markup displayed to the user.
  • Controller (fetches Model, contains business logic) - responsible for handling HTTP requests, the request calls the method, and renders the view with the data.
  • HTTP Request and Response.
ASP.NET Routing Process
  1. GET request: Students/Index
  2. ASP.NET routes request to StudentsController action.
  3. View Engine locates, renders, and returns the View.

Routing (in detail)

  • Routes are stored in the route table.
  • The routing engine checks if the URL matches a route in the route table.
Example
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
  • Explanation:
    • {controller=Home}: Specifies the default controller as "Home".
    • {action=Index}: Specifies the default action as "Index".
    • {id?}: Specifies an optional parameter named "id".

Routing – Assignment

  1. Create a new project: ASP.NET Core Web App (Model-View-Controller).
  2. When you start the application and type /contact/me in the URL, the text “Contact me” should be displayed.

Routing – Solution

  • Create a new controller named ContactController in the Controllers folder.
  • Create a new view named Me in the Views/Contact folder.

Middleware

  • Middleware is a bridge between two parts of your application.
  • It is an intermediate layer that performs actions on every request.
    • Client sends an HTTP Request.
    • Middleware processes the request.
    • Middleware sends an HTTP Response.
    • Server receives the response.
Middleware Pipeline
  • Multiple middlewares are executed by default.
  • Standard Middlewares:
    • ExceptionHandler
    • HSTS
    • HttpsRedirection
    • Static Files
    • Routing
    • CORS
    • Authentication
    • Custom middlewares
    • Authorization
    • Custom1
    • Endpoint

Middleware – Assignment

  • Use the same project as the previous assignment.
  • Open the program.cs class and examine the middlewares that are called.
  • What happens if you comment out a line and start the application?

Middleware – Solution

  • Commenting out middleware may not immediately appear to change anything.
  • The application might continue to function, and the /contact/me page may still be accessible.
  • The WebApplicationBuilder automatically packages all configured middleware with UseRouting.
Why Explicitly Define Middleware in Code?
  • You can control when the middleware is executed.
  • This is useful when adding custom middleware.

Homework: Showcase

  • Develop the profile page (CV) according to the given documentation.
  • Ensure that companies can see who you are and what you are good at.

Optional Homework

  • Practice with routing and HTTP methods.
  • See the practice assignment with routing on Brightspace.
  • Contactformulier.zip

Preparation for the Next Lesson

  • Watch the following videos in the given order (see ‘Les 1. REST API’ on Brightspace):
    • What are APIs?
    • What is a REST API?

Next Lesson

  • REST API