ASP.NET Core

Getting started with ASP.NET Core 2.1

This post is just an introductory post on the ASP.NET Core application. In this post, we’ll create an ASP.NET Core application and we’ll examine the folders and code.

If you missed my previous post on ASP.NET Core and Benefits. Take a look.

Prerequisites:

To run/create applications for .NET Core we need the following things

  • Visual Studio 2017
  • .NET Core 2.1 SDK (for the purpose of this article only).
  • Make sure Visual Studio 2017 has the “cross-platform development” workload is installed through visual studio installer

Creating our application:

For creating .NET Core applications open the Visual Studio 2017 and select File >> New >> Project. And this should launch the Templates wizard like this.

Select as highlighted in the below screenshot.

The ASP.NET Core Web application template holds several other templates. Select the appropriate template and framework and hit ok.

New Project

Now, this should create our application. Notice that we also have templates for Angular, React and Redux in the above screen.

Let’s run the application. Press F5 or Ctrl + F5.

up and running

There it is. Our application is up and running.

Let’s examine the folder structure now. Here is what our solution looks like

folder structure in asp.net core project

As you can see we also have wwwroot folder along with MVC structure. The wwwroot folder will have static files such as CSS, JS, HTML, images. This is called as Web root.

To access these files, the URI format is

http://serveraddress/foldername/filename

Let’s try to access one image.

accessing a static image

Program.cs

Here is the screenshot of the code

program.cs

This is just having a Main method to run the app with the default host builder. Notice we have a CreateWebHostBuilder property.

WebHost.CreateDefaultBuilder method sets the content root to the current directory.

The Program.cs also sets the startup using .UseStartup method.

Startup.cs

Let’s see what we have in the Startup.cs class

namespace Coderethinked.Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

The ConfigureServices method is called by the runtime as indicated in the comments above the method. This method can be used to add services.

Ex:

services.AddAntiforgery();
services.AddCors();
services.AddAuthorization();
services.AddAuthentication();

There are several other options to add into this Configure services method.

Configure:

This method is used to configure what the application needs.

Notice the configure method here, we have the following statement

app.UseStaticFiles();

This is responsible for serving the static files in our application.

If we comment that out, we cannot access the static resources anymore. We’ll see all our images, js and CSS will be stopped and our application looks like this.

with no static file access

Things like SignalR, Routing, Third party authentication can be configured here. That’s about the Startup file.

Launch Settings:

This JSON file is to configure holds the configurations to run the application.

launch settings

As we are running the application on IIS Express, the necessary configuration for IIS Express is utilized.

"iisSettings":{
   "windowsAuthentication":false,
   "anonymousAuthentication":true,
   "iisExpress":{
      "applicationUrl":"http://localhost:51681",
      "sslPort":44307
   }
}

In the above JSON, we have applicationUrl: http://localhost:51681 and a sslPort: 44307 is allocated. So, 44307 is for SSL port meaning this is for accessing the site with HTTPS://. So https://localhost:44307 is secured while http://localhost:51681 is just a plain HTTP which is not secured.

So, HTTP and HTTPS are configured by default.

The other folders like Controllers, Models, Views are with any other MVC projects we have encountered before. Not found anything special in those files

Thanks for reading.

Disqus Comments Loading...
Share
Published by
Karthik Chintala

Recent Posts

2 Good Tools To Test gRPC Server Applications

In this post, we’ll see how to test gRPC Server applications using different clients. And… Read More

2 years ago

Exploring gRPC project in ASP.NET Core

In this post, we'll create a new gRPC project in ASP.NET Core and see what's… Read More

2 years ago

Run dotnet core projects without opening visual studio

In this blog post, we’ll see how to run dotnet core projects without opening visual… Read More

2 years ago

Programmatically evaluating policies in ASP.NET Core

Programmatically evaluating policies is useful when we want to provide access or hide some data… Read More

2 years ago

Multiple authorization handlers for the same requirement in ASP.NET Core

We saw how we could set up policy-based authorization in our previous article. In this… Read More

2 years ago

Policy-Based Authorization in ASP.NET Core

What is policy-based authorization and how to set up policy-based authorization with handlers and policies… Read More

2 years ago

This website uses cookies.