Book Image

Mastering ASP.NET Core 2.0

By : Ricardo Peres
Book Image

Mastering ASP.NET Core 2.0

By: Ricardo Peres

Overview of this book

<p>ASP.NET is an open source web framework that builds modern web apps and services. This book is your one-stop guide to the new features of ASP.NET Core 2.0, including web APIs and MVC. We begin with a brief overview of the basics, taking you through the MVC pattern, platforms, dependencies, and frameworks. We then move on to setting up and configuring the MVC environment before talking about routing and advanced routing options. Next, we'll look at model binding, controllers and actions, filters, user authentication, and testing.</p> <p>Moving on, you’ll learn about all the aspects of syntax and processes when working with Razor. You’ll be introduced to client-side development and will get to know about the security aspects of ASP.NET Core. We will also look at Microservices with ASP.NET Core. Finally, you’ll find out how to deploy ASP.NET Core to new environments such as Azure, AWS, and Docker. By the end of the book, you will be well versed with development in ASP.NET Core and will have a deep understanding of how to interact with the framework and work cross-platform.</p>
Table of Contents (23 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Knowing the environments


.NET Core has the notion of 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 (case-sensitive):

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

To be correct, 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, extensions methods and properties of the IHostingEnvironment interface:

  • 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 on the code, such as pick 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 IHostingEnvironment from the Configure method:

    public void Configure(IApplicationBuilder app,
     IHostingEnvironment env) { /* ... */ }

But also from the constructor of the Startup class:

    public Startup(IHostingEnvironment env) { /* ... */ }

Or even from the dependency injection framework, which is available from the HttpContext class, among other places:

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

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. A nice feature that can help us better organize our code!