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

Knowing the environments

.NET Core has the concept of the environment. An environment is basically a runtime setting in the form of an environment variable called ASPNETCORE_ENVIRONMENT. This variable can take one of the following values (note that these are case sensitive):

  • Development: A development environment, which probably does not need much explaining
  • Staging: A preproduction environment used for testing
  • Production: An environment (or as similar as possible) in which the application will live once it is released

To be specific, you can pass any value, but these have particular significance to .NET Core. There are several ways by which you can access the current environment, but you're most likely to use one of the following methods, extension methods and properties of the IWebHostEnvironment interface (add a using reference to the Microsoft.Extensions.Hosting namespace):

  • IsDevelopment()
  • IsProduction()
  • IsStaging()
  • IsEnvironment("SomeEnvironment")
  • EnvironmentName

The IsDevelopment, IsProduction, and IsStaging extension methods are just convenience methods using the IsEnvironment method. Based on the actual environment, you can make decisions about the code, such as picking a different connection string, web service URL, and so on. It is important to point out that this has nothing to do with debug or release compiler configurations.

You normally get an instance of IWebHostEnvironment from the arguments to the Configure method of the Startup class:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... }

But you also get it from the DI container, which is available from the HttpContext class, among other places, as the RequestServices property:

var env = HttpContext.RequestServices.GetService<IWebHostEnvironment>();

Or you can just inject IWebHostEnvironment into your controller as the following:

public IActionResult Index([FromServices] IWebHostEnvironment env) { ... }

This allows you to check your current environment any time, so that you have conditional logic.

The IWebHostEnvironment replaces the old IHostingEnvironment interface available in pre-3 .NET Core, now deprecated.

A final note: service configuration plays well with environments. Instead of a single ConfigureServices method, we can have multiple methods, named ConfigureDevelopmentServices, ConfigureStagingServices, and ConfigureProductionServices. To be clear, any environment name can be added after the Configure prefix and before Services. The environment-specific method (for example, ConfigureDevelopmentServices) will be called instead of the generic one (ConfigureServices):

public void ConfigureDevelopmentServices(IServiceCollection services)
{
//WILL be called for environment Development
}

public void ConfigureServices(IServiceCollection services)
{
//will NOT be called for environment Development
}

And, if we want to take it a bit further, we can even do the same for the Startup class: we can create one class per environment, with it as the suffix:

public class StartupDevelopment
{
public StartupDevelopment(IConfiguration configuration) { ... }

public void ConfigureServices(IServiceCollection services) { ... }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { ... }
}

Or, if we want to dynamically specify a class that resides in a different assembly, we'll have to slightly change the code in the Program class, so as to bootstrap from an assembly:

publicstaticIHostBuilder CreateHostBuilder(string[] args) =>    Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup(typeof(Startup).Assembly.FullName);
});

We can do it from an assembly instead of from a specific class:

publicstaticIHostBuilder CreateHostBuilder(string[] args) =>    Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(builder =>
{
builder.UseStartup<Startup>();
});

A nice feature that can help us better organize our code! Let's now have a look at the standard project templates that we can use to start creating our projects.

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