Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Monday, 2 May 2016

Action Filter In MVC

What are the filters:
Sometimes we would like to perform certain action before or after a particular operation, or some times we need a pre or post action behaviors from action, for achieving this functionality ASP.NET MVC provides a feature called Filters.

Types of filters:

ASP.NET MVC supports following types of filters:

  1. Action Filters
  2. Authorization Filters
  3. Result Filters
  4. Exception Filters
  5. Action Filters: 
Action filters are used to implement the logic that get executed before or after a controller action executes.

Authorization Filters:

It is used to implement authorization and authentication for action filters

Result Filters:
Result filters contains logic that gets executed before or after a view result gets executed. E.g. if you want to change view before its get render to browser.

Exception Filters:
Exception filters are used to handle error, caused by either controller action or controller action results, we can also use it for logging the exceptions.


Let's start with Action Filter in ASP.NET MVC:
Action Filters can be applied to either controller action or controller itself, with the help of action filter we can change the way, the action or controller gets executed.

MVC provides following action filters:
  • Output Cache:

    Output
    This filter caches the output of action for certain duration. E.g. below code snippet, we are decorating login action with output cache keyword, that will cache the output of login action for 20 seconds( we have given 20 seconds duration).
  • Handle Error:
    It handles the error caused by action or controller, if any exception occurs it redirects the action to custom error page. e.g: here in the below code snippet handle error attribute is decorated to login action method, it will redirect to a view called "error.cshtml" when any exception will occur by this action method.
    Handle
  • Authorize:
    It is used for filtering the authorized user to access the resource. E.g. in below code snippet we have decorated an action method with Authorize attribute.
    Authorize
  • If we will try to access this action then it will give following error:
    error
So these are the action filters, hopefully I was able to satisfy you, more will come with the next article on custom filters.

Monday, 1 February 2016

Custom HTML Helpers In MVC

HTML Custom Helpers

HTML helper is a method that returns a HTML string. Then this string is rendered in view. MVC provides many HTML helper methods. It also provides facility to create your own HTML helper methods. Once you create your helper method you can reuse it many times.

There are two ways in MVC to create custom Html helpers as below.
  1. Adding extension method for HtmlHelper class
  2. Using static method
Adding extension method for HtmlHelper class

We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers.

Example

Add new class in MVC application and give it meaningful name. In this example we create new folder “CustomHelper” in application and then we add class “CustomHelpers” in that folder. As we are going to create extension method make this class static. Write the following code in class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace CustomeHelper.CustomHelper  
  8. {  
  9.     public static class CustomHelpers  
  10.     {  
  11.         public static IHtmlString File(this HtmlHelper helper, string id)  
  12.       {  
  13.             TagBuilder tb = new TagBuilder("input");  
  14.             tb.Attributes.Add("type""file");  
  15.             tb.Attributes.Add("id", id);  
  16.             return new MvcHtmlString(tb.ToString());  
  17.         }  
  18.     }  
  19. }  
In above code we create static method file for HtmlHelper class. In this method we create html tag with the help of TagBulider class. In this code we just use two attributes you can add more html attributes as per your requirement. Now we use this html helper method on view just like inbuilt helpers. As this class resides in “CustomHelper” namespace we should add this namespace in view as below,
  1. @using CustomeHelper.CustomHelper  
  2. @ {  
  3.     ViewBag.Title = "Index";  
  4. }  
  5.   
  6. < h2 > Index < /h2>  
If you want to use these helpers on multiple views then add namespace in web.config file. Once you add that namespace you are ready to use “File” helper like below,

file

Add this method and pass id parameter. And run your view, you will get output as below.

output

Using static method

In this method we don’t create any extension method for HtmlHelper class. We create new class as “CustomHelper”. Write the following code in class:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace CustomeHelper.CustomHelper  
  8. {  
  9.     public class CustomHelper  
  10.     {  
  11.         public static IHtmlString File(string id)  
  12.       {  
  13.             TagBuilder tb = new TagBuilder("input");  
  14.             tb.Attributes.Add("type""file");  
  15.             tb.Attributes.Add("id", id);  
  16.             return new MvcHtmlString(tb.ToString());  
  17.         }  
  18.     }  
  19. }  
Once you create this you are able to use this helper method on view. As in this method we don’t create extension method for HtmlHelper class, we don’t use like inbuilt helpers. To use this helper we use class name instead of “@Html” (in razor view) like below.

class

Add this method and pass id parameter. And run your view, you will get output as below.

output

Conclusion

In this article we learned to create custom html helpers. By creating custom html helper we can eliminate the repeating html code fragments from application.