Thursday, November 22, 2018

Specified argument was out of the range of valid values. Parameter name: site

Specified argument was out of the range of valid values. Parameter name: site

While development many time we face this issue.It come generally when we change pc or laptop ,we just install visual studio and sql server and start our project.We forget to enable IIS setting so this error comes.
Solution : 
Please open 
 Control Panel -> Programs -> Programs and Features -> Turn Windows features on or off -> Internet Information Services and check the Internet Information Service if unchecked.

Image for reference :

Monday, November 19, 2018

Action Filters in asp.net MVC

What are Action Filters in asp.net MVC?

ASP.NEt MVC provides a very clean way of injecting the pre-processing and post-processing logic for actions and controllers. We can put the pre-processing and post-processing logic is by decorating the actions with attributes which will invoke an attribute class implementing  the filter's logic.

For example, If we need some action to be executed when the user has authenticated after that we can adorn the action with the [Authorize] attribute. This implements the authorization filter to check whether the user has is 
authorized or not.

[Authorize]
public ActionResult Index()
{
    return View();

}

Actually action Filters are additional attributes that can be applied to either a controller or the entire controller to modify the way in which action is executed. These attributes are special .NET classes derived from System.Attribute which can be attached to  classes, methods, properties and fields.

MVC provides the following action filters:
Output Cache: This action filter caches the output of a controller action for a specified amount of time.
Handle Error: This action filter handles errors raised when a controller action executes.
Authorize: This action filter enables you to restrict access to a particular user or role.

Types of filters

 
ASP.NET MVC supports following types of filters:
  1. Action Filters
  2. Authorization Filters
  3. Result Filters
  4. Exception Filters
Action filter
Action filter will be called before and after the action starts executing and after the action has executed. We can put our custom pre-processing and post-processing logic in this filter.

Now to implement this filter we need to create a custom filter attribute class and implement the

IActionFilter interface
This interface provides us two methods OnActionExecuting and OnActionExecuted which will be called before and after the action gets executed respectively.

public class CustomActionAttribute : FilterAttribute, IActionFilter
{
    void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
    {
        filterContext.Controller.ViewBag.OnActionExecuted = "IActionFilter.OnActionExecuted filter called";
    }

    void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewBag.OnActionExecuting = "IActionFilter.OnActionExecuting filter called";
    }
}

Authorization filter
This filter provides authentication and authorization logic. It will be executed before the action gets executed. To implement this action the interface IAuthorizationFilter should be implemented by the custom attribute class.

public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
{
    void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
    {
        filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called";
    }
}


Result filter
Result filter will execute before and after the result of the action method has been executed. We can use this filter if we want some modification to be done in the action's result.

IResultFilter interface
This interface provides two methods OnResultExecuting and OnResultExecuted which will be called before and after the action result respectively.

public class CustomResultAttribute : FilterAttribute, IResultFilter
{
    void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)
    {
        filterContext.Controller.ViewBag.OnResultExecuted = "IResultFilter.OnResultExecuted filter called";
    }

    void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)
    {
        filterContext.Controller.ViewBag.OnResultExecuting = "IResultFilter.OnResultExecuting filter called";
    }
}

Exception filter
Exception filter will be invoked whenever a controller or action of the controller throws an exception. This is particularly useful when we need custom error logging module.

To implement this filter we need to create a custom filter attribute class which implements IExceptionFilter. This interface gives us a methods called OnException which is a perfect place to call the exception logging module and to redirect to some error page.

public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{       
    void IExceptionFilter.OnException(ExceptionContext filterContext)
    {
        filterContext.Controller.ViewBag.OnException = "IExceptionFilter.OnException filter called";
    }
}

Order of Execution of action filter as following

IAuthorizationFilter.OnAuthorization
IActionFilter.OnActionExecuted
IActionFilter.OnActionExecuting
IResultFilter.OnResultExecuted
IResultFilter.OnResultExecuting
IExceptionFilter.OnException