Thursday, July 9, 2015

difference between save and insert in mongodb



If the document contains an _id field, then the save() method performs an upsert querying the collection on the _id field:
If a document does not exist with the specified _id value, the save() method performs an insert with the specified fields in the document.
If a document exists with the specified _id value, the save() method performs an update, replacing all field in the existing record with the fields from the document.

Tuesday, May 26, 2015

Child Actions in ASP.NET MVC


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:
This child action returns a partial view with the current time as the data in the partial view. The partial view is created as below:
CurrentTime.cshtml
In the main view, the child action can be used as shown below:
Above we are using child action from the same controller in which the main action is defined. To use child actions from a different controller, you have the option of passing the controller name as well. You can also pass input parameters to the child action. To demonstrate this, let us create the child action with an input parameter as follows:
In the view, a parameter can be passed to the child action as follow:
You may consider using the child action method in the following scenarios,
  • To use data in the view which is not related to the main controller
  • To create data driven widgets to be used on different views

Custom Action Filter in ASP.NET MVC 5


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
I have tried to show the filter execution timing in context of request processing in the below diagram:
customfilterimg1
Action Filter
An action filter consists of codes that run either before or after an action runs. It can be used for tasks like logging, privileged based authorization, authentication, caching etc.
Creating a custom action filter is very easy. It can be created in four simple steps:
  1. Create a class
  2. Inherit ActionFilterAttribute class
  3. Override the OnActionExecuting method to run logic before the action method
  4. Override the OnActionExecuted method to run logic after the action method
Let us see how we can create a custom action filter. The purpose of the action filter is to find whether a logged in user belongs to a particular privileges or not. On the basis of the result, a user will access a particular action or navigate to the login action. To do this, I have created a class and extended the ActionFilterAttribute class.
As you see in the above code, the OnActionExecuting method is overridden because we want the code to execute before the action method gets executed. Once the action filter is created, it can be used in three ways:
  1. As Global filter
  2. As Controller
  3. As Action
By adding a filter to the global filter in App_Start\FilterConfig it will be available globally to the entire application.
By adding a filter to a particular Controller it will also be available to the all actions of that particular controller.
By adding a filter to a particular action it will be available to the particular action.
1
2
3
   [AuthorizationPrivilegeFilter]
        public ActionResult About()
        {

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...