Tuesday, October 18, 2016

Action Method in Mvc

In this section, you will learn about the action method of controller class.

All the public methods of a Controller class are called Action methods. They are like any other normal methods with the following restrictions:

Action method must be public. It cannot be private or protected
Action method cannot be overloaded
Action method cannot be a static method.
The following is an example of Index action method of StudentController

Action Method in ASP.NET MVC
Action Method
As you can see in the above figure, Index method is a public method and it returns ActionResult using the View() method. The View() method is defined in the Controller base class, which returns the appropriate ActionResult.

Default Action method:

Every controller can have default action method as per configured route in RouteConfig class. By default, Index is a default action method for any controller, as per configured default root as shown below.

Default Route:

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{name}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional
});

However, you can change the default action name as per your requirement in RouteConfig class.

ActionResult:

MVC framework includes various result classes, which can be return from an action methods. There result classes represent different types of responses such as html, file, string, json, javascript etc. The following table lists all the result classes available in ASP.NET MVC.


Result Class

Description
ViewResult Represents HTML and markup.
EmptyResult Represents No response.
ContentResult Represents string literal.
FileContentResult/ FilePathResult/ FileStreamResult Represents the content of a file
JavaScriptResult Represent a JavaScript script.
JsonResult Represent JSON that can be used in AJAX
RedirectResult Represents a redirection to a new URL
RedirectToRouteResult Represent another action of same or other controller
PartialViewResult Returns HTML from Partial view
HttpUnauthorizedResult Returns HTTP 403 status

The Index() method of StudentController in the above figure uses View() method to return ViewResult (which is derived from ActionResult). The View() method is defined in base Controller class. It also contains different methods, which automatically returns particular type of result as shown in the below table.


Result Class

Description

Base Controller method
ViewResult Represents HTML and markup. View()
EmptyResult Represents No response.
ContentResult Represents string literal. Content()
FileContentResult,

FilePathResult,

FileStreamResult
Represents the content of a file File()
JavaScriptResult Represent a JavaScript script. JavaScript()
JsonResult Represent JSON that can be used in AJAX Json()
RedirectResult Represents a redirection to a new URL Redirect()
RedirectToRouteResult Represent another action of same or other controller RedirectToRoute()
PartialViewResult Returns HTML PartialView()
HttpUnauthorizedResult Returns HTTP 403 status

Action method Parameters:

Every action methods can have input parameters as normal methods. It can be primitive data type or complex type parameters as shown in the below example.

Example: Action method parameters

[HttpPost]
public ActionResult Edit(Student std)
{
// update student to the database

return RedirectToAction("Index");
}

[HttpDelete]
public ActionResult Delete(int id)
{
// delete student from the database whose id matches with specified id

return RedirectToAction("Index");
}

Action method parameters can be Nullable type.

By default, the values for action method parameters are retrieved from the request's data collection. The data collection includes name/values pairs for form data or query string values or cookie values. Model binding in ASP.NET MVC automatically maps the URL query string or form data collection to the action method parameters if both names are matching. Visit model binding section for more information on it.

ASP.NET MVC Version History


Microsoft had introduced ASP.NET MVC in .Net 3.5, since then lots of new features have been added.
The following table list brief history of ASP.NET MVC.



MVC Version
Visual Studio .Net Version Release date Features
MVC 1.0 VS2008 .Net 3.5 13-Mar-2009
  • MVC architecture with webform engine
  • Routing
  • HTML Helpers
  • Ajax Helpers
  • Auto binding
MVC 2.0 VS 2008, .Net 3.5/4.0 10-Mar-2010
  • Area
  • Asynchronous controller
  • Html helper methods with lambda expression
  • DataAnnotations attributes
  • Client side validation
  • Custom template
  • Scaffolding
MVC 3.0 VS 2010 .Net 4.0 13-Jan-2011
  • Unobtrusive javascript validation
  • Razor view engine
  • Global filters
  • Remote validation
  • Dependency resolver for IoC
  • ViewBag
MVC 4.0 VS 2010 SP1,

VS 2012
.NET 4.0/4.5 15-Aug-2012
  • Mobile project template
  • Bundling and minification
  • Support for Windows Azure SDK
MVC 5.0 VS 2013 .NET 4.5 17-oct-2013
  • Authentication filters
  • Bootstrap support
  • New scaffolding items
  • ASP.Net Identity
MVC 5.2 - Current VS 2013 .NET 4.5 28-Aug-2014
  • Attribute based routing
  • bug fixes and minor features upate

Thursday, October 13, 2016

Angular Tutorial - Part 1: Introduction to Angular.js


In this article we will look at the basics of Angular.Js. This is the first part of an article series. The main objective of this series is to learn about Angular and see how Angular helps us in developing more flexible, maintainable and testable client side web applications and that too without compromising with the productivity.

Link to complete series



Angular Tutorial - Part 1: Introduction to Angular.js
Angular Tutorial - Part 2: Understanding Modules and Controllers
Angular Tutorial - Part 3: Understanding and using Directives
Angular Tutorial - Part 4: Understanding and implementing Filters
Angular Tutorial - Part 5: Understanding and implementing Services
Angular Tutorial - Part 6: Building and Validating Data Entry Forms
Angular Tutorial - Part 7: Understanding Single page applications and Angular routing
Background
If we take a look back in time and see how our software applications have evolved then we can clearly see how the application development and development model has evolved. Almost two decade ago most software applications were getting built as standalone applications. These applications were targeted at a single user and ran on their operating systems.

Then came the need to share data across multiple users and a need to store data at a central location. This need gave birth to distributed applications. Distributed applications ran as standalone applications on the user’s machine giving him a rich user interface (les call it desktop like experience) to work with, and behind the scenes, these applications sent data to a server.

Read More