Skip to main content

Explore Action Filters in Asp.Net MVC Applications

What is Action Filters

In the Asp.Net MVC application there are controllers, each controller defines action methods that generally have one to one relation ship with views or each actions use performs on the page.
User actions means we can say like clicking on buttons, links or submitting a form. When this kind of action performs request is routed to controller and finds the action method.
Now if we want to perform some action, calculate login or check some values either before calling an action method or after action method completes Action Filters are used.

Asp.Net MVC provides actions filters which we can say custom attributes which provides facility to perform some behavior at pre-action or post-action time of controller action method.

Now lets see the available types of the Action filter in Asp.Net MVC

Action Filter Types

Asp.Net MVC provides 4 types of action filters like Authorization filter, Action filter, Result filter and Exception filter. Please find the below description for each type of action filter.

Authorization filter

Generally it is used to implement authorization and authentication for action method, It is related to security of the application whether to execute action method or not, it also check for the authentication and request property.
As an example you can see AuthorizeAttribute class for Authorization action filter.

Action filter

Generally it is used to perform some extra processing, like before executing any action method we want to check like user is logged in or not, or user has enough permission to execute this action method or not.
If session expired and we want to transfer request to login page etc.

Result filter

Generally it is used to perform extra processing of the result like modifying the response that means if we want to execute a login before or after the view result get executes we can use this filters.
As an example you can see OutputCacheAttribute class for result filter.

Exception filter

It executes when ever there is an unhanded exception thrown by anywhere in the action method. Exception filters can be used for the tasks like logging an error or displaying an error page 404
As an example you can see HandleErrorAttribute class for exception filter.

Serialization & Deserialization using JSON In C#


The very useful method of converting JSON object to .Net object and .Net object to Json object is using the JsonConvert. It converts Json objects to .Net object and with the Same property with value and .Net objects to equivalent JSON object.

In this article we will see how to serialize .NET type objects into JSON object and then deserialize data in the JSON format back into .NET types using the JsonConvert class.

What is JSON?

JSON (JavaScript Object Notation) is one lightweight and efficient data exchange format or we can say like JSON is most desirable data encoding format which allows fast exchanges of small amounts of data between client browsers and web services or web api.


JsonConvert class is in the namespace “Newtonsoft.Json” and for this namespace you need to add reference of Newtonsoft.Json dll or nuget package. You can install a Newtonsoft.Json nuget package by command “Install-Package Newtonsoft.Json” in package manager console.

If we want to convert .Net object to Json object then the SerializeObject() method will be used and when we want to convert Json object back to .Net object then DeserializeObject() method will be used from the JsonConvert class

JSON Serialization

Now lets see the JSON Serialization process with code example, we have an instance of Customer class as below

public class Customer
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNo { get; set; }

Now create an object of Customer class and initilize value of each properties. Then call SerializeObject() of JsonConvert class with passing Customer object.
See the below codesnippet

// Serializaion .Net Object
Customer customer = new Customer();
customer.Id = 1;
customer.Name = "Tarun Dudhatra";
customer.Email = "";
customer.PhoneNo = "5*9*9 5*8*5";

// Convert Customer object to JOSN encoded format
string jsonDataString = JsonConvert.SerializeObject(customer);


It will return the JSON string as below

{"Id":1,"Name":"Tarun Dudhatra","Email":"","PhoneNo":"5*9*9 5*8*5"}

JSON Deserialization

See the below JSON Deserialization process with code example, we have same Customer class string as below

{"Id":2,"Name":"FirstName LastName","Email":"","PhoneNo":"5*9*9 5*8*5"}

Now call the DeserializeObject() of JsonConvert class and pass the object type of Customer so it will deserialize it into Customer Class.
See the below codesnippet

Deserializaion JSON Object

string JSONObject = @"{ 'Id':2,'Name':'FirstName LastName','Email':'','PhoneNo':'5*9*9 5*8*5'}";

Customer customer = JsonConvert.DeserializeObject<Customer>(JSONObject);

Console.WriteLine( "Id : " + customer.Id);
Console.WriteLine( "Name : " + customer.Name);
Console.WriteLine( "Email: " + customer.Email);
Console.WriteLine( "Phone: " + customer.PhoneNo);

It will print the below out put on the screen

Id : 2
Name : FirstName LastName
Phone: 5*9*9 5*8*5

That’s it, in my upcoming article we will see other interesting stuff like this.

SQL Server Interview Question and answers

If you are going for an SQL Server Interview or .Net interview then you need to refer below list of questions. Below question and answer will give you a break through. Here I have prepare some simple and tricky questions for SQL Server which will really help you to go through the interview.
1. What is the difference between clustered and a non-clustered index?
– Clustered index is the physical index, It reorders the records physically in the table while storing the data.
– Non-Clustered index is the logical index, It does not reorders the data into table, it’s just set the logical order of the index.

2. What is the difference between UNION and UNIONALL in SQL Server?
– Union and Union All both are used in select statement, Union will remove the duplicate rows from the final result set of the select statement while UNIONALL will not remove duplicate row from the result set and will return all the row. So you can use UNION clause if you want to avoid data redundancy.

3. What’s the difference between a primary key and a unique key?
– Unique key and Primary key both are constraints in SQL Server but has little bit difference, Both keys are not allow to store duplicate value in the database, but Primary key is a cluster index and Unique key is a non clustered index. Another difference is Primary key can not contains any null value while Unique key contains only 1 null value.

4. Alter command to change the data type of a column
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
Below is the query to change column datataype

ALTER TABLE tablename ALTER COLUMN columnname data type

Let’s say we want to change the datatype from int to string of phone number column of employee table then below is query for it.

ALTER TABLE employee ALTER COLUMN phonenumber varchar(15)


5. In which Files does SQL Server Actually Store Data?
On the disk SQL server has 2 data files associated with each database
1. LDF : Which stores generally transaction log
2. MDF : Which stores actual data

6. What is CHECK Constraint?
– CHECK Constraint used to limit the values that can be inserted in a table column

7. Do you know How many locks are exist in SQL Server ?
– We have these many locks exist here, see below
1. Intent
2. Shared
3. Update
4. Exclusive
5. Schema
6. Bulk Update

8. Can I insert value into a table which having just one IDENTITY column?
– Yes we can, There is a very simple query for that as below


9. How to drop primary key from a column using Query
– It’s very simple you can use Alter table command for that, see the below query to drop primary key constraint
ALTER TABLE tablename DROP CONSTRAINT PK_tablename_columnname
in above query PK_tablename_columnname is the name of primary key constraint.

10. What is a DDL trigger and a DML trigger?
– DDL commands are used to create, modify or delete the structre of the table, like creating table, alter table or drop table, so we can easily say those are the command which deal with the table structure. Those commands are CREATE, ALTER and DROP
– DMS commands are used to insert, update or delete the data of in to the table, like insert into, update and delete from, so those are the commands which deal with the table data, Those commands are INSERT, UPDATE and DELETE.

11. What is the difference between Trigger and Stored Procedure?

– Triggers and Stored procedure are generally same but the only single difference is that trigger cannot be called directly, it can be called on either insert,update or delete statement of the table based on trigger we define.

12. How can you delete duplicate records in a table where there is no primary key?
– You can take Use of the SET ROWCOUNT command. Here if you had 2 duplicate rows you would issue SET ROWCOUNT 1, then your DELETE command then SET ROWCOUNT 0.

13. How to check the version of SQL server and operating system?
– With the help of the following query we can get it

SELECT SERVERPROPERTY (‘productversion’) as Version, SERVERPROPERTY (‘productlevel’) As Level, SERVERPROPERTY (‘edition’) as Edition


14. What is NOT NULL Constraint?
– NOT NULL is the constraint in the SQL Server. When ever we want to force someone to enter data into particular table column then we need to set it NOT NULL column so it will not accept NULL value.
15. Find the 3rd MAX Age of the employee in the emp table
– With the help of the following query we can get 3rd Max Age
Select distinct age from emp e1 where 3 = (select count(distinct age) from emp e2 where e1.age <= e2.age);

16. How to copy data from one table to another table?
– With the help of ‘INSERT INTO SELECT’ or ‘SELECT INTO’ queries you can copy data from a data-table to another

17.What is the Sixth normal form in SQL server?
– Actually Sixth normal form exist but use it when you want a relational system in conjunction with time. At this moment SQL Server does not support it directly.

18.What is SQL Profiler?
– It is SQL Server tool that facilitate administrator to monitor different events and transaction of particular SQL server instance, You can capture and save data about each event to a file or SQL Server table to analyze later.
It is not available with EXPRESS edition

19. What is PIVOT in SQL Server
– To sort, total and count the data stored in 1 table automatically we use PIVOT. It will also rotate table as a columns to rows and rows to column.
– To automatically sort, count, and total the data stored in one table we use PIVOT. It will also rotate table as rows to columns and vice versa

Please let us know your suggestion/queries