Sunday, August 2, 2020

Recent questions asking in interview - C# , asp.net , asp.net mvc , asp.net core , sql , entity framework

 Q. Sql Injection 
 Q. Sql cache 
 Q. Exception Filter purpose
Q. MVC Validations - Jquery ka use yaha par 
Q. Await Async




1. What is the difference between SingleOrDefault and FirstOrDefault?

LINQ provides element operators that return a single element or a specific element from a collection. The elements operators are Single, SingleOrDefault, First, FirstOrDefault, Last, LastOrDefault.

Single

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection.
SingleOrDefault

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.

First

It returns first specific element from a collection of elements if one or more than one match found for that element. An exception is thrown, if no match is found for that element in the collection.

FirstOrDefault

It returns first specific element from a collection of elements if one or more than one match found for that element. A default value is returned, if no match is found for that element in the collection.

When to use Single, SingleOrDefault, First and FirstOrDefault

You should take care of following points while choosing Single, SingleOrDefault, First and 
FirstOrDefault:
When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
When you want a default value is returned if the result set contains no record, use SingleOrDefault.
When you always want one record no matter what the result set contains, use First or FirstOrDefault.
When you want a default value if the result set contains no record, use FirstOrDefault.

Perfomance of SingleOrDefault and FirstOrDefault

FirstOrDefault usually performs faster as compared to SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterates the whole collection to find one single match.

Q.2 AllowAnonymous Attribut and Authorize Attribute.?

One of the new features in ASP.NET MVC 4 is the AllowAnonymous Attribute that helps you secure an entire ASP.NET MVC 4 Website or Controller while providing a convenient means of allowing anonymous users access to certain controller actions, like the login and register Actions.


AllowAnonymous Attribute in ASP.NET MVC 4

As mentioned, if you create a new ASP.NET MVC 4 Internet Project in Visual Studio 2010 or Visual Studio 11 and view the AccountController you will notice the generous use of the AllowAnonymous Attribute on various login and register controller actions. Here are a few of those controller actions.

[Authorize] 
public class AccountController : Controller 
 [AllowAnonymous] 
 public ActionResult Login() 
 // ... 
 } 
 [AllowAnonymous]
 [HttpPost] 
 public JsonResult JsonLogin(LoginModel model, string returnUrl) 
 // ...
 }
 [AllowAnonymous]
 [HttpPost] 
 public ActionResult Login(LoginModel model, string returnUrl) 
 // ... 
 }
 [AllowAnonymous] public ActionResult Register() 
 // ... 
 } 
 [AllowAnonymous] 
 [HttpPost] 
 public ActionResult JsonRegister(RegisterModel model)
 {
 // ... 
 }
 [AllowAnonymous]
 [HttpPost] 
 public ActionResult Register(RegisterModel model) 
 // ... 
 } 
}

The idea is pretty simple. The Authorize Attribute on the AccountController in this ASP.NET MVC 4 Application denies anonymous access to every controller action. However, we need to allow anonymous access to the login and register controller actions so we decorate them with the AllowAnonymous Attribute which negates the Authorize Attribute and allows anonymous access.

Q.3 What is mvc Filters and custom filters . ?


ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.

Q.4 What is Dependency injection with examples . ?

In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. In the typical "using" relationship the receiving object is called a client and the passed object is called a service.


No comments:

Post a Comment

Rewrite whole Web Site System and Sub System with data migration to new system.

Can you please share me your skype id or whats up number for better communications  my skype id is - jitendra.tech  whats up - +919617741414...