Monday, 8 February 2016

Caching in MVC5 ?

Caching in MVC5

Caching is the most important aspect of high-performance web application.Caching is way of storing data when fetching of data is frequent hence praticlly imprving performnce of application.

Advantages of Caching
  • Reducing round trips
  • Reducing Server database round trips
  • Better Performance
  • Lesser Network Traffic
For Caching in MVC we use Output Cache Filter.

Output Cache Filter
This filter will allow us to cache the data for any action method for the specified time, by default the time limit is 60 seconds. This means when any cal is made the data will be cached for next 60 minute after that 60 seconds if there is any call the data will be cached again.
Example of using Output Cache Filter

[OutputCache(Duration=20, VaryByName="none")]
public ActionResult Index()
return View();

Here the code will cacche the index page for next 20 seconds. We can check the cache effect by placing the ViewBag.Message on the index view page.The result will remian the same for the next 20 seconds.

Output Cache Location
This is vary usable depending upon the usage. Now with caching there will be problem if we want to cache the personal data of the user, If the data is cached on the server the details remain the same for other users also. Thus there is location parameter in the Output Cache Filter.

You can set the Location property to any one of the following values:
  • Any
  • Client
  • Downstream
  • Server
  • None
  • ServerAndClient

Sunday, 7 February 2016

Initializing variable in ?

We are learning a new language daily so we need to remember how too initialize variables in so bookmark this and use it as your daily guidebook to initialize variable in

Initialize variables in

Single Dimensional Array 
int[] n1= new int[4]{2,3,1,4};
int[] n2= new int[]{1,2,3,4};
int[] n3= {1,2,3,4,};

string[] s1= new string[4]{"tom","jerry","harry","ben"};
string[] s2= new string[]{"tom","jerry","harry","ben"};
string[] s3= {"tom","jerry","harry","ben"};

Multi-dimensional array.
int[,] n1 = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
int[,] n2 = new int[,] { {1, 2}, {3, 4}, {5, 6} };
int[,] n3 = { {1, 2}, {3, 4}, {5, 6} };

string[,] n1 = new string["ben","tom"] { {"sam","joe"}, {"harry","jack"}, {"noa","nemo"} };
string[,] n2 = new string[,] { {"sam","joe"}, {"harry","jack"}, {"noa","nemo"}};
string[,] n3 = {{"sam","joe"}, {"harry","jack"}, {"noa","nemo"} };

Jagged array.
int[][] n1 = new int[2][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
int[][] n2 = new int[][] { new int[] {2,4,6}, new int[] {1,3,5,7,9} };
int[][] n3 = { new int[] {2,4,6}, new int[] {1,3,5,7,9} };

string[][] n1 = new string[2][] { new string[] {2,4,6}, new string[] {1,3,5,7,9} };
string[][] n2 = new string[][] { new string[] {2,4,6}, new string[] {1,3,5,7,9} };
string[][] n3 = { new string[] {2,4,6}, new string[] {1,3,5,7,9} };

Filters in MVC5?

Filters: Filters in mvc are a way to implement cross cutting concerns which means Functionaity that is used aross the application based on different layers. These fnctionality includes logging, exception handling, cacheing, Authorization, Authentication etc.

These function are required to kept at single place so as to mkae changes easier.Advavntage that filters provide as they use common logic to implemented using cross cutting concerns that can be applied on different controllers and action methods.

There are generally two types of filters that are Global filters and Controller filters. 

We can create a Global filter by creating a class nad registering it as a global filter 
public class DemoGlobalFilterAttribute : ActionFilterAttribute
       public override void OnActionExecuting(ActionExecutingContext context)
           context.RequestContext.HttpContext.Response.Write("This is global filter");

Then register global filter
GlobalFilters.Filters.Add(new TestGlobalFilterAttribute());

Similary we can do for Controller Filters.

Filter Overrides in MVC 5: 
MVC5 gives a feature of filter override which can be applied to controllers and action methods which can override the golbal filter or the controller filter for the specified controller or the action method.

lets take a small example we want to override the contactus action method in the home controller.for this we only need to apply the OverrrideActionFilter attrinbute on the action method.Check the code below. 
public ActionResult ContactUs(){
ViewBag.Message = "This is the contact us page";
return View();
This can be usefull in different and multiple scenarios, Lets see a small scenario.
public class ProdController : Controller  

public ActionResult GetMobiles(string categoryId)
    ViewBag.categoryId= t;      
    return View();

Here we have applied the authorize filter to the controller, then overridden the controller filter on GetMobiles action action method

We have filter overrides for each filter types
OverrideActionFilter         -  ActionFilter
OverrideAuthorizationFilter  -  AuthorizationFilter
OverrideAuthenticationFilter -  authenticationFilter
OverrideExceptionFilter      -  ExceptionFilter

Saturday, 6 February 2016

Attribute based routing in MVC 5 ?

Attribute Based Routing:Attribute based routing can defined as route configuration in the same place where action method is defined. This is way of setting the route configuration for every single action method in same section where action is defined for better usage and fast configuration.

To enable Attribute based routing we need to add a small section into the RouteConfig.cs file,

public static void RegisterRoutes(RouteCollection routes)

Optional Parameter: We can specify the optional parameters in the URL in route attribute only using "?" character.

public ActionResult GetElectronicItems(int? id) {
ViewBag.Id = id; return View();

Route Constraints:  Specify parameter constraints placing the constraint name after the parameter name separated by colon.Here the action methods needs a id of int type to be worked upon.


Route Prefix: If there are a lot of action in a controller , then we can choose different prefix for them using Route Prefix attribute.


So now our Route attribute on our action method does not need to specify the common prefix