Category Archives: ASP.NET Core 1.0

I think we all are familiar with the configuration of the default startup page in the previous versions of AP.NET but it’s slightly different in ASP.NET Core Applications. In this article, I will explain how to configure the default startup page In ASP.NET Core.

Default Startup Page Configuration

In this way, we can implement the default startup page In ASP.NET Core.

Default Configuration

Customized Configuration

Default Configuration

We can use UseDefaultFiles() extension method in ASP.NET Core. UseDefaultFiles() will only search for the files given in “wwwroot”. If any of the files are detected first in “wwwroot” the files are run as default in the client Browser.

default.html

default.htm

index.html

index.htm

UseDefaultFiles must be called before UseStaticFiles or Anyother method(app.Run,app.Use) to serve the default file in the client-side Browser. As you mention UseStaticFiles() method after UseDefaultFiles(), it will run UseStaticFiles() method as a default and automatically terminates the other files, which come after UseStaticFiles() method.

Customized Configuration

In this case, we are calling other customized pages as default startup pages in ASP.NET Core. Thus, we can use DefaultFilesOptions in ASP.NET Core. If you want to run other files as default, check the code give below in Startup.Cs.

In this article, we will explain how to create a “Session State in ASP.NET Core and MVC Core”.

Session State

In Session State, we can use to save and store user data while the user browses your web app. We already know that in previous versions of ASP.NET, we could store session as key value pair like this “Session[“Name”] = “Rajeesh Menoth”” and implement it in an easy way. But in the latest version of ASP.NET or ASP.NET Core, we need to do a few configurations for accessing and enabling Session State in the application. The main purpose of session is maintaining user data in memory because of HTTP is a stateless protocol.

Before reading this article, you must read the articles given below for ASP.NET Core knowledge.

We add the “app.UseSession()” inside the Configure Method in “Startup.cs” Class because it gets called by the runtime. One more advantage is we can use this method to configure the HTTP request pipeline in our application.

We are going to add “UseMvc” Middleware and “AddMvc()” Configure Services in Startup.cs Class. The code given below clearly mentions that manually we need to add our controller name and an action name in “MapRoute”. We can change this controller name and action name, which is based on our requirement in the Applications.

In the previous versions, everything is handled by Web.Config but in ASP.NET Core, the connection string is written in appsettings.json file. By default, it will show as a LocalDB path and as per our requirement, we can change the connection string path.

The appsetting JSON file contains the LocalDB connection string details in our Application and we have given the database name as “RegDB”. If you want to know more about JSON file configuration in ASP.NET Core, please check our previous article ADDING A CONFIGURATION SOURCE FILE IN ASP.NET CORE 1.0.

We are going to implement Code First Migrations in an Entity Framework Core to seed the database with the test data and manually we are entering the seed data information in the “RegContextSeedData” class. The code given below contains the default values of Registration Details Table in our Application.

The simple way in which we can say Bower is optimized for the front-end in our Applications and it provides the client side dependencies. For example, Bower manages the components and it contains HTML, CSS, JavaScript, fonts or even the image files.

In the previous versions, everything is handled by Web.Config but in ASP.NET Core, the connection string is written in appsettings.json file. By default, it will show as a LocalDB path and as per our requirement, we can change the connection string path.

In the way given below, we can create connection string in ASP.NET Core 1.0.

Appsetting in Asp.Net Core

The appsetting JSON file contains the LocalDB connection string details in our Application and we have given the database name as “UserDb”. If you want to know more about JSON file configuration in ASP.NET Core, please check our previous article ADDING A CONFIGURATION SOURCE FILE IN ASP.NET CORE 1.0

We are going to implement Code First Migrations in Entity Framework Core to seed the database with test data and manually we are entering the seed data information in the “CodeDbSeedData” class. The code given below contains the default values of User Details Table in our Application.

We learned about Code First Migration in ASP.NET Core MVC 6 with EntityFrameWork Core, using Command Line Interface ( CLI ) and I hope you liked this article. Please share your valuable suggestions and feedback.

There are several articles on Google for creating Rest API or Web API with ASP.NET Core 1.0. In this article, I will explain how to create Rest API or Web API with ASP.NET Core 1.0, starting from scratch.

Before reading this article, you must read the articles given below for ASP.NET Core knowledge.

We choose “Empty” template in “ASP.NET Core Templates” Category. I think someone may have a doubt like — without selecting a “Web API”, why do we choose “Empty” template in “ASP.NET Core Templates” Category ? Because the “Web API” templates automatically generate a few libraries related to REST API creation. So, we don’t know what is happening in the background. That’s why we choose “Empty” template.

Selecting Empty Template in Asp.Net Core 1.0

References Required

We need the following references for accessing Static files, libraries for Routing, and Rest API, accessing MVC design pattern, etc.

The route will assign a single route path for accessing specific API Controller CRUD operations. Instead of “[controller]”, we can mention our controller name as “LibraryAPI” in client side or server side code. If searching for any information from API, we can pass id like this -“api/LibraryAPI/id”.

Accessing Json data into LibraryAPI

At client-side, we are going to access JSON data from Library API with the help of JavaScript.

In the following code, we mention the “AddMvc()” in configuration service method. It will help to access the MVC related information at runtime.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace DotNetCoreExtentions
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseFileServer();
app.UseMvc();
}
}
}

Click “show all files icon” in Solution Explorer. You can see the jspm_packages reference and config.js inside the wwwroot.

Aurelia Project Structure In ASP.NET Core 1.0

Aurelia Reference in index.html page

We are going to add Aurelia references in “index.html” page because in ASP.NET Core 1.0, we run index.html file as a start page in default. Don’t worry. We can mention other pages also but we need to do some customization in the code.

Index.html

The code given below contains the reference of “jspm_packages/system.js” & “config.js”. The body tag contains the “aurelia-app” or an entry point of Aurelia app and it automatically detects the app.js file in our root folder (what we mention baseURL on jspm installation) or wwwroot. The “System.import(“Aurelia-Bootstrapper”)” will help to import Aurelia-Bootstrapper references in the body.

We have created one more staticfile or app.js inside the wwwroot or Webroot. The file given below contains the syntax as “export”. It will help to export the entire class details to correspondence app.html as Controller and View name relation in ASP.NET MVC.

The code given below in UseFileServer() will help to call staticfiles in ASP.NET Core 1.0.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace AureliaHelloWorld
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseFileServer();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}