How is ASP.Net Web Forms architecture different from ASP.Net MVC?

Web forms uses page controller or base controller design pattern where as MVC uses Front Controller design pattern.

Page Controller:

Page receives request from IIS and call controllers method. In traditional web forms all pages (View) inherit from System.Web.UI.Page class which has a code behind class acting as a controller. Continue reading

What are various Results returned by Action Methods in MVC ?

Actions in Asp.Net MVC returns ActionResult which is base class for all the results that can be returned. Below are classes that derive from ActionResult and can be returned as a result.

  • ViewResult
  • PartialViewResult
  • ContentResult
  • JsonResult
  • JavascriptResult
  • EmptyResult
  • FileResult
  • FilePathResult
  • FileContentResult
  • FileStreamResult
  • HttpNotFoundResult
  • HttpStatusCodeResult
  • HttpUnAutorizedResult
  • RedirectResult
  • RedirectPermanentResult
  • RedirectToActionResult
  • RedirectToActionPermanentResult
  • RedirectToRouteResult
  • RedirectToRoutePermanentResult

Continue reading

What’s the difference between const and readonly?

const are compile time constants where as readonly are runtime constants. Once assigned you cannot change the values assigned to variables declared as const or readonly.

Usage:

Const variable just like their static(shared in VB) counterparts are accessible via Class Names only. They are not accessible via objects.

Readonly variable are instance variable and accessible via objects. Continue reading

Automation in .Net Part 2

HttpRequest and HttpResponse Classes

Let’s try to scrape from data from my own blog i.e. www.shaktitanwar.com . We will try to extract all blog titles on home page.

There are various facets to scraping:

  1. We need to first have a URL that we need to scrape i.e. shaktitanwar.com
  2. What all data needs to be scraped? In our case it’s Blog titles.
  3. Which verb is used by the URL to show data? In our case it’s a GET request
  4. The HTTP Headers required by website to serve the content properly.

Continue reading

OOPS In VB.Net – Encapsulation – Abstraction

Introduction
OOPS or Object Oriented Programming Structures. Some of the concepts have been always used in one or the other programming languages. For example you must have used structs in C which is a good example of encapsulation. There are four major pillar of OOPS. Let’s try to understand each one of them by taking some examples also:-
1.) Encapsulation

2.) Abstraction

3.) Polymorphism

4.) Inheritance

Continue reading

Node.Js Part 1

Node.Js

 

On popular demand from many of my student and fellow developers, I am starting this series on Node.js . Focus will be on developing Node.js applications using Visual studio and side by side explaining things about other open source frameworks that go hand in hand with Node e.g. Bower, Grunt, Yeoman, Jasmine etc.

What is Node.js?

Node.js is a server side JavaScript framework. A server side framework means not only web framework like PHP,Asp.net but also something using which you can create your own servers like Apache or IIS. Using Node.js we can create networking applications on server as well. Unlike some other frameworks Node.js is completely Open source. Continue reading

Asp.Net MVC 5 – Scaffolding Enhancements

Scaffolding Enhancements

Scaffolding is a feature through which we can generate code automatically using some predefined templates. Asp.Net MVC has built in scaffolding support and feature has been enhanced in MVC 5 by introduction of New Scaffold Menu item.

Let’s try to generate a controller for an existing Model class

public class PersonModel
{
[Key]
public int PersonId { get; set; } 

public string PersonName { get; set; } 

public int PersonAge { get; set; }
}

Continue reading

Asp.Net MVC 5 – Authentication Filters

Authentication Filters

One of a very common question asked in interview is difference between Authentication and Authorization.

Authentication is Who all are allowed to enter site.

Authorization is What all they are allowed to access once they have entered the site.

Before MVC 5 we already had Authorization Filters which we use to control access to resources i.e. whether the current user principle has access to requested resource on server. Continue reading

Asp.Net MVC 5 – Override Filters

Override Fiters

Asp.net MVC 5 provides a way to control which filter applies to which controller or action methods. This can be achieved using Override Filters.

As you might be aware that Filters can be applied to controllers or Actions or Global level. We can apply filters at higher levels usually globally or at controller level and then depending on use we can override them at a lower level i.e. Controller and Action respectively.

Let’s apply Authorize attribute which is an Authorization Filter at Global Level.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
 filters.Add(new AuthorizeAttribute(){Roles = "Member"});
}

Continue reading

ASP.Net MVC 5 – Attribute Based Routing Part 1

Attribute Based Routing:

Routing is a process of mapping a virtual or nonexistent URL to a physical resource on server. Url Routing is not a new feature and traces of routing were always available in asp.net web forms from version 2.0.

If you have worked on web forms in asp.net 2.0 than you must remember the attribute called UrlMapping in web.config. Just to recap how that worked

<system.web>
    
    <urlMappings>
      <add url="~/cricket" mappedUrl="~/Home/Cricket.aspx"/>
    </urlMappings>
</system.web>

Continue reading