Child Actions in ASP.NET MVC

Read full post on falafel blog

Child Actions are the action methods which can be invoked within the view. This is used to work with the data in the view, which are not related to the main action method. For example, if you want to create a data driven widget in the view using data that is not related to the main action method, you will need to use the child action method.
In ASP.NET MVC any action can be used as a child action. However, to use an action only as a child action and attribute it with the ChildActionOnly. It will make sure the action is not called by any user request and will only be used in the view. A child action can be created as shown below:

Read full post on falafel blog

Check User exists in a Role in ASP.NET Identity 2.0

Read full post on Falafel blog

ASP.NET MVC5 uses Identity 2.0 to manage the users and their roles. There may be scenario to check whether the currently logged in user or any user with the id exists in the given role or not, and checking can be done either in the controller class or in any other class of the application.

Check for the currently logged in user
In a controller you can find whether the currently logged in user exist in a role or not by using User property of the controller class. Type of the User property is IPrincipal and it has a Boolean method IsInRole which takes the role name as input and returns true if the currently logged in user exist in the role.

Read full post on Falafel blog

Start-up code using WebActivator in MVC application

Read full post on Falafel blog

WebActivator allows us to execute a code long before application start-up. There may be requirements when you need to load external assemblies or instantiate a particular object before the application start up. You need to make sure some codes execute before the codes inside the App_Start folder and the global.asax file. To do this WebActivator should be used.
You can add the WebActivator package using NuGet.

Let us consider an example in an MVC application in which we want to call a method of a class before  the execution of the application. To do this follow the steps below:

  • Attribute the class with WebActivator
  • Pass the type of the class
  • Pass the name of the method to be executed

Read full post on Falafel blog

Seed the database with initial Users in MVC 5

 

Read full article on Falafel blog

In MVC 5 applications you may want to seed the database with initial users for various reasons. You may want default users and roles added as part of the application. In this post, we will take a look at how to  seed the database with default users.
The MVC 5 application uses ASP.Net Identity 2.0 to manage users, roles and identities. ASP.Net Identity 2.0 uses the Entity framework code first approach to create a database. To seed the database, first you need to enable migration on the database. To enable migration, in the package manager console run the command enable-migrations as shown below:

After successfully enabling migration you should find a Migrations folder in the solution explorer.

 

Read full article on Falafel blog

Custom Action Filter in ASP.NET MVC 5

Read full article on Falafel blog here

ASP.NET MVC 5 provides five different kinds of Filters. They are as follows:

  1. Authentication [Introduced in MVC5]
  2. Authorization
  3. Action
  4. Result
  5. Exception

Filters are used to inject logic at the different levels of request processing. Let us understand where at the various stages of request processing different filters get applied.

  • Authentication filter runs before any other filter or action method
  • Authorization filter runs after Authentication filter and before any other filter or action method
  • Action filter runs before and after any action method
  • Result filter runs before and after execution of any action result
  • Exception filter runs only if action methods, filters or action results throw an exception

Read full article on Falafel blog here

Convert User to ApplicationUser in ASP.Net MVC

In the last post I talked about How to get current user in ASP.Net MVC 5 . Type of the current user is IPrincipal which is shown as follows:

image

You may come across requirement to work with ApplicationUser which is created as follows:

image

You can covert returned User to ApplicationUser as follows:


ApplicationDbContext db = new ApplicationDbContext();
            ApplicationUser appUser = db.Users.FirstOrDefault(x => x.Id == userId);
            return appUser ;

To convert create instance of the ApplicationDBContext and then fetch user using LINQ to Entity. Returned user from context is of type ApplicationUser.

Hope it helps. Happy coding.

How to get current user in ASP.Net MVC 5

There are two simple ways to get current user in MVC 5. If you are inside the controller class current user can be fetched as follows,


string userId = User.Identity.GetUserId();

Do not forget to add namespace,

 


using Microsoft.AspNet.Identity;

Other scenario could be that you are not inside the controller class and want to fetch the user information. You can fetch that using HttpContext class.

HttpContext.Current.User.Identity.GetUserId();

I hope it helps. Happy Coding.