Adding Web API Controller To Existing MVC Project

If you have MVC project and you need to add Web API controller to this project, it can be done very easy.

1. Add Nuget package Microsoft.AspNet.WebApi.Core. This will add references to System.Web.Http.dll.

2. Add registration file for Web API. It would better to do it like Visual Studio does – add file App_Start\WebApiConfig.cs

3. Add using System.Web.Http; to Global.asax.cs

4. Add GlobalConfiguration.Configure(WebApiConfig.Register); in Application_Start() method before registering MVC routes.

Application_Start() would be like this.

5. Create a controller and derive it from System.Web.Http.ApiController

ASP.NET Identity. Part 2 – Customizing Entities

ASP.NET Identity gives a class ApplicationUser for storing data about users. It has a lot of properties defined in base class IdentityUser, such as Id, UserName, Email, PhoneNumber, etc. But when you need to add some extra properties, it becomes more complicated.

Entity Framework creates a database only once at first use. It does not recreate, nor modify it. We should use EF migrations to add new fields. Also it gives the possibility to change table names.

1. Add new properties in file Models\IdentityModels.cs

2. Open Package Manager Console and run command

EF will create folder Migrations and a couple of files: Configuration.cs and <timestamp>_InitialCreate.cs with code to create a default database structure.
3. Create new migration and give it a name

Visual Studio will open a file with two method to add and drop columns defined in step 1.
4. Apply EF migration to database.

Now I have the following database structure:

5. I want to enter first and last names during the registration process. So I’ll add two properties to class RegisterViewModel in file Models\AccountViewModels.cs

6. Add code to Views\Account\Register.cshtml file to display these them.

7. Change Register method in Account controller (file Controllers\AccountController.cs) to store them.

8. Also I want to display user’s first name on the top bar of the web application instead of his/her email. To do this we need to modify file Views\Shared\_LoginPartial.cshtml to get ApplicationUser via current user’s Id with the help of Owin context, then to show property FirstName. The final code looks like this:

ASP.NET Identity. Part 1 – Changing Default Database

When you create MVC project in Visual Studio 2017, you can use individual user accounts authentication that is shipped with MVC template. This implementation uses Entity Framework. By default, EF creates a database in SQL Server LocalDB with a name like aspnet-WebApp-<timestamp>.

To change the connection to database:
1. Open Web.config file
2. Find section <connectionStrings>
3. Modify the attribute connectionString in element with name DefaultConnection. Element will look like the following:

<add name="DefaultConnection" connectionString="Data Source=(local);Integrated Security=SSPI;Initial Catalog=AspNetIdentityDB;" providerName="System.Data.SqlClient"/>

If you dislike the name DefaultConnection, you should change it in Web.config file and in the constructor of ApplicationDbContext class (file Models\IdentityModels.cs).

Encrypting appSettings Key In ASP.NET Web.config

I needed a solution to encrypt a single key in appSettings section in ASP.NET Web.config file. The article on MSDN Encrypting and Decrypting Configuration Sections tells about encrypting the whole section but I want to hide only a particular key.
It seems that apsnet_regiis could encrypt only sections, so I’ve found a way how to extract this key to another section and encrypt that section in one of StackOverflow answers

Here are the steps that must be done to get the result:
1. Add a line in configSections with the name of your new section

2. Add new section under configuration

3. In .NET code get the section and key from it

4. After you deploy your Web application to IIS, you should encrypt section secureAppSettings according to the MSDN article mentioned above. Launch Command Prompt as Administrator (it’s important!) and run command (something like this):

MVC. Placing Javascript Files In View Folder

When you need to split layout and JS code to different files, it might be convenient to place both files in Views/{ControllerName} folder. You need to change web.config file inside Views folder.

1. Add handler in <system.web> section

2. Add handler in <system.webServer> section before BlockViewHandler

Now you can include JavaScript file in your layout page.