Use An Area In ASP.NET Core

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core hosting.  In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the Startup.cs file (It’s best to place it before any non-area route).

In Startup.cs, configure the method.

Then, make a folder named Areas in the app root and make another directory named Admin inside the former. Inside the admin folder, create the following folders (ViewComponent is optional).

Now, we will create a Controller inside the Controllers folder named AdminController.

Now, in order for that to work, you’ll need to create Views for all actions that return one. The hierarchy for Views is just like what you have in a non-area Views folder.

Question – What if I want to have another Controller inside my Area?

Answer 

Just add another Controller beside AdminController and make sure the routes are like the following,

The important part is [Route(“admin/[controller]”)]. With that, you can keep the style of routing to admin /controller/ action/.

Using multiple hosting environments on the same machine in ASP.NET Core

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core hosting. This short post about how to set the hosting environment in ASP.NET Core.

However, if this is a capability you think you will need, you can use a similar approach to the one I use in that post to set the environment using command line arguments.

This approach involves building a new IConfiguration object, and passing that in to the WebHostBuilder on application startup. This lets you load configuration from any source, just as you would in your normal startup method, and pass that configuration to the WebHostBuilder using UseConfiguration. The WebHostBuilder will look for a key named "Environment" in this configuration, and use that as the environment.

For example, if you use the following configuration.

 You can pass any setting value with this setup, including the “environment variable”:

This is fine if you can use command line arguments like this, but what if you want to use environment variables? Again, the problem is that they’re shared between all apps on a machine.

However, you can use a similar approach, coupled with the UseEnvironment extension method, to set a different environment for each machine. This will override the ASPNETCORE_ENVIRONMENT value, if it exists, with the value you provide for this application alone. No other applications on the machine will be affected.

To test this out, I added the MYCOOLPROJECT_ENVIRONMENT key with a value of Staging to the launch.json file VS uses when running the app:

Running the app using F5, shows that we have correctly picked up the Staging value using our custom environment variable:

With this approach you can effectively have a per-app environment variable that you can use to configure the environment for an app individually.

Summary

On shared hosting, you may be in a situation when you want to use a different IHostingEnvironment for multiple apps on the same machine. You can achieve this with the approach outlined in this post, building an IConfiguration object and passing a key to WebHostBuilder.UseEnvironment extension method.

How To Using Node Services In ASP.NET Core

How To Using Node Services In ASP.NET Core

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core Hosting. This post is about running Javascript code in the Server. Because a huge number of useful, high-quality Web-related open source packages are in the form of Node Package Manager (NPM) modules. NPM is the largest repository of open-source software packages in the world, and the Microsoft.AspNetCore.NodeServices package means that you can use any of them in your ASP.NET Core application.

aspnet-mvc-poster


To use Node Services, first you need to include the reference of Microsoft.AspNetCore.NodeServices package in your project file. You can do this using dotnet add package Microsoft.AspNetCore.NodeServices command.

Then you need to add the Node Services middleware to the request pipeline. You can do it in your ConfigureServices() method.

Now you’re able to get instance of INodeServices in your application. INodeServices is the API through which .NET code can make calls into JavaScript that runs in a Node environment. You can use FromServices attribute to get the instance of `INodeServices’ in your action method. Here is Add method implementation in MVC.

And here is the code of AddModule.js file

You need to use the type of the result in your InvokeAsync method, in this example I am using int. NodeServices allows ASP.NET Core developers to make use of the entire NPM ecosystem, which gives rise to a huge range of possibilities. You can find the full source code on GitHub.

Happy Programming.

ASP.NET Core Identity Tables Customization With Visual Studio 2017

ASP.NET Core Identity Tables Customization With Visual Studio 2017

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core 1.0 hosting. In this article, we will use Visual Studio 2017 and Entity Framework Core step by step to learn how we can:

  1. Change Identity table names.
  2. Change Identity table property names.
  3. Change Identity table datatype of primary key.

When you decide to use ASP.NET Core Identity it will generate some tables used to store data for users and other things.

Prior to it, let’s take a brief definition about ASP.NET Core Identity.

Released on 2016 and from the name we can say it’s all about identity of the users, you can manage login and Logout functionality, create an account also you can use external login providers such as Facebook, Google, Microsoft Account and Twitter. In other words, you can say, it gives you the ability to make an authentication and an authorization.

Let us see it in action

Open your Visual Studio 2017

image001

I am using an Enterprise edition but you can use any edition, which is available to you.

File > New > Project

Click ASP.NET Core Web Application and give it a name, as shown below.

image002

Select Web Application.

image003

Click Change authentication, select an individual user account and click OK, as shown below.

image004

Now, we have an Application but we need to make migration and we have several ways to achieve it. We will use Package Manager, as shown below

image005

Now, you need to create the database. Go to appsettings.json file and edit your connection string, if you want. I am using SQL Server 2016, so this is my appsettings.json file and it looks, as shown below.

Now, in Package Manager console, write Add-migration command, followed by the name of this migration command like this and wait for some seconds.

image006

Now, update the database and click enter.

image007

Now, go to the database and refresh it. You will find aspnet-IdentityConfig database with some tables created by default like AspNetUsers table.

We will use this table to achieve our goal.

image008

Now, how can we change this table name from AspNetUsers to AspUsers as example.

It’s database stuff, so we can achieve this from ApplicationDbContext class.

image009

To be specific from OnModelCreating method into this class, we can customize our ASP.NET Identity model and override the defaults like change table name, property name and others.

We will change the table name now from AspNetUsers to AspUsers by this code inside OnModelCreating method. Proceed, as shown below.

Note- After any customization in the tables, you should make database migration and update it to reflect the changes.

First, write add-migration. Subsequent to the migration, update the database.

image010

Now, refresh the database and you will find the name changed to AspUsers.

image011

Change Identity table property name

We need to change as an example Email to EmailAddress in AspUser table

image012

It’s very similar to what we did before, just we will add the code to the OnModelCreating

In this code we told entity to get Email property and change this column name to EmailAddress

Now you should make a migration to reflect your changes on Database and we will use it as usual

Add-Migration command then Update-Database

image013

Now it’s time to look in the database

image014

Change Identity table datatype of primary key

We will need to make some changes in ApplicationUser class so go to this class and open it

image015

ApplicationUser class contains all the custom information I want to store about a user, also inheritance here from IdentityUser gives me all essentials like username ,id, password property or Email, that means if I want to add any property, I will add it here in ApplicationUser class

By default the primary key is string

image016

Because IdentityUser has the primary key  we want to pass our new primary key datatype.

Where will we pass this new datatype? Look to the picture below

image017

As you can see we need to pass our primary key as example int datatype to IdentityUser<int> .

The code will look like this

And because every user has roles we will need to make this change in roles also by creating new class ApplicationRole which will inherit from IdentityRole class and pass the new datatype to it, and the code will look, as shown below.

Afterwards, we will make some change in our ApplicationDbContext class in order to open it.

image018

We need to add our new role and datatype IdentityDbContext to the class. It will look, as shown below.

In the last step, we will need to reconfigure ConfigureServices method in the startup class.

We need to make some changes in these lines.

We need to replace IdentityRole class to our role ApplicationRole class and add our new datatype to AddEntityFrameworkStores<ApplicationDbContext,int>

The code will look, as shown below.

Now, we have all the required changes achieved but the last step will be the migration.

This time, we have some new things like when you run add-migration command; you will see the warning, as shown below.

image019

When you try to run update-database, you will get an error told to you.

To change the IDENTITY property of a column, the column needs to be dropped and recreated.

Thus, we will drop the database and create it again.

Step 1

Drop the database command to remove the database and press Y.

Step 2

Remove all the migrations, which you have in Visual Studio.

Step 3

Run add-migration command.

Step 4

Run update-database command.

Now, go to the database to see our changes.

image020

You see now that id has int datatype and not nvarchar.

Summary

Now, you can change an Identity column name, Identity table name, Identity primary key. I hope this is helpful to you.