Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Modern Web Development with ASP.NET Core 3
  • Table Of Contents Toc
Modern Web Development with ASP.NET Core 3

Modern Web Development with ASP.NET Core 3 - Second Edition

By : Peres
3.7 (6)
close
close
Modern Web Development with ASP.NET Core 3

Modern Web Development with ASP.NET Core 3

3.7 (6)
By: Peres

Overview of this book

ASP.NET has been the preferred choice of web developers for a long time. With ASP.NET Core 3, Microsoft has made internal changes to the framework along with introducing new additions that will change the way you approach web development. This second edition has been thoroughly updated to help you make the most of the latest features in the framework, right from gRPC and conventions to Blazor, which has a new chapter dedicated to it. You’ll begin with an overview of the essential topics, exploring the Model-View-Controller (MVC) pattern, various platforms, dependencies, and frameworks. Next, you’ll learn how to set up and configure the MVC environment, before delving into advanced routing options. As you advance, you’ll get to grips with controllers and actions to process requests, and later understand how to create HTML inputs for models. Moving on, you'll discover the essential aspects of syntax and processes when working with Razor. You'll also get up to speed with client-side development and explore the testing, logging, scalability, and security aspects of ASP.NET Core. Finally, you'll learn how to deploy ASP.NET Core to several environments, such as Azure, Amazon Web Services (AWS), and Docker. By the end of the book, you’ll be well versed in development in ASP.NET Core and will have a deep understanding of how to interact with the framework and work cross-platform.
Table of Contents (26 chapters)
close
close
1
Section 1: The Fundamentals of ASP.NET Core 3
7
Section 2: Improving Productivity
14
Section 3: Advanced Topics
1
Appendix A: The dotnet Tool

Getting your context

You will probably remember the HttpContext class from ASP.NET. The current instance of this class would represent the current context of execution, which included both the request information and the response channel. It was ubiquitous, and even though in Web Forms it was sometimes hidden, it was the way by which the web application communicated with the client.

Of course, ASP.NET Core also has an HttpContext class, but there is a big difference: there is no longer a Current static property that lets us get hold of the current context—instead, the process is a bit more convoluted. Anyway, all of the infrastructure classes—middleware, controllers, views, Razor pages, view components, tag helpers, and filters—allow easy access to the current context. Those who don't can leverage the IHttpContextAccessor interface through DI and get a pointer to the current context:

//this is required to register the IHttpContextAccessor
//services.AddHttpContextAccessor();

...

public MyType(IHttpContextAccessor httpContextAccessor)
{
var httpContext = httpContextAccessor.HttpContext;
}

So, besides User, Request, and Response properties, which are mostly similar to their pre-Core counterparts, we also have the following:

  • A Features collection, which exposes all of the features implemented by the current hosting server (Kestrel, WebListener/HTTP.sys, and more).
  • A RequestServices property, which gives us access to the built-in DI framework (more on this in the following chapters).
  • A TraceIdentifier property, which uniquely identifies a request in ASP.NET Core 2.x; in earlier versions, we had to access this through a feature.
  • A Connection object, from which we can obtain relevant information about the client connection, such as the client certificates, for example:
    • The Authentication object, giving easy access to security primitives, such as sign in, sign out, deny, and more.
    • The Session object, which is implemented by the ISessionFeature feature, and is exposed directly by the HttpContext.
    • The ClientCertificate property contains any SSL certificate sent by the client as part of the handshake protocol.

The context is a vital part of an ASP.NET Core application, as we will see.

Working with the context

The main operations we will likely be doing with the context are as follows:

  • Reading values from the request
  • Writing to the response
  • Reading and writing cookies
  • Getting the current user
  • Getting the address of the remote user
  • Accessing the session
  • Accessing services from the DI framework

Here are some examples:

//writing to the response
HttpContext.Response.StatusCode = 200;
HttpContext.Response.ContentType = "text/plain";
HttpContext.Response.WriteAsync("Hello, World!");

//getting values from the request
var id = HttpContext.Request.Query["id"].Single();
var host = HttpContext.Request.Host;
var payload = HttpContext.Request.Form["payload"].SingleOrDefault();

//reading and writing cookies
var isAuthenticated = HttpContext.Request.Cookies["id"].Any();
HttpContext.Response.Cookies.Append("id", email);

//getting the current user
var user = HttpContext.User;

//getting the address of the remote user
var ip = HttpContext.Connection.RemoteIpAddress;

//accessing the session
HttpContext.Session.SetString("id", email);
var id = HttpContext.Session.GetString("id");

//getting services from DI
var myService = HttpContext.RequestServices.Get<IMyService>();

Essentially, everything we will be doing through constructs, such as MVC's controllers and actions, are built around these and other simple HttpContext operations. The next topic we will look at is the OWIN pipeline.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Modern Web Development with ASP.NET Core 3
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon