Controller in 2 ASP.Net MVC Application

No.of Views805
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : ASP.NET , How to
Controller in 2 ASP.Net MVC Application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 Objective:

Purpose of this tutorial is to explain all about Controller in ASP.Net MVC application.
Part 1 of this series of tutorial could be found here

ASP.Net MVC controller

1. MVC controller takes user request in browser.
2. These are responsible for responding to request made against ASP.Net MVC web site.
3. Browser request is mapped to a Controller.

Explaning with example

When runing a MVC dafult website , the browser request get maped in below manner. Like below in address bar of browser address is

Image Loading....



Url in browser is http://localhost:1589/Home/About

So mapping would be like below

Home -> Conroller class
About -> Action ( Method inside HomeController class)


So on seeing , HomeController class , there is a method called About.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";

return View();
}

public ActionResult About()
{
return View();
}
}
}


{/codecitation}

Action

1. These are the public method inside a Controller class. Through these methods user interacts to ASP.Net MVC web site. These methods are exposed to outside world.
2. Any Action could be invoked from outside just by typing its address in address bar of browser
3. Action return type must be ActionResult. It might have any other retutn type also like string or intger. But if it is not ActionResult, return data will get convrted into string and then get render.
ActionResult

These are the results returend by the Actions. Action result is result which controller returns in response of browser request.

These are types of action result supported by MVC Controller.
All types of action result is inherited from ActionResult base class.

Image Loading....

Note : action result are not get called directly. Instead of them method of controller base class get called.

List of methods of Controller base class is as follows

Image Loading....

Example of returning a JSON action result

Step 1:

Create a new MVC web application. Select File->New->Project->Web->ASP.Net MVC web application.

Image Loading....

Step 2:

{codecitation class="brush: csharp; gutter: true;" width="650px"}


Open HomeController class and add the below code there
public ActionResult List()
{
List<int> r = new List<int>();
for (int i = 0; i < 100; i++)
{
r.Add(i);
}
return Json(r);
}

{/codecitation}

So now Home Controller class will look like ( HomeController class is in Controller folder)

HomeController.cs

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";

return View();
}

public ActionResult About()
{
return View();
}

public ActionResult List()
{
List<int> r = new List<int>();
for (int i = 0; i < 100; i++)
{
r.Add(i);
}
return Json(r);
}

}
}


{/codecitation}

Step 3:

Run the application by pressing F5. Home page of MVC will get open. Type below URL in the address bar of browser
http://localhost:1549/Home/List
Note : port number will be different , when you run it on different system. For my system it is 1549.
The below dialog box will pop up.

Image Loading...

Save this some where and try to open that file in NOTEPAD or something like that. All number from 0 to 99 will be there.

Image Loading...

How to create a Controller ?

Image Loading....

Method 1:

By Right clicking on Controller folder and selecting Add then New Controller


Step 1:

Image Loading....

Step 2:

Remove Default to any signifiacnt name. here name is UST.
Note : Ecah Controller class must append with keyword Controller. So make sure there is no space while renaming default to other name.

Image Loading....

Image Loading...

If check box is checked the by default framework will create stub for Create and Update and Details. Here it is unchecked.
Now USTController should be listed in sollution expolere inside Controller folder.

Image Loading...


Step 3:

By default below class will get created


HomeController.cs

{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace MvcApplication1.Controllers
{
public class USTController : Controller
{
//
// GET: /UST/

public ActionResult Index()
{
return View();
}

}
}

{/codecitation}

Here Action colud get added later. This could be access on browser as
http://localhost:1987/UST

Method 2:

Creating a manual class inside Controller folder and derive this class from Controller base class.

Step 1:


Right click on Controller and add new class by clicking on add.

Step 2:

Give name but make sure , it is appended with Controller. For example if desired name of controller is csharp then here name will be
csharpController. So name of the class will be csharpController.cs

Step 3:

Add namespace


{codecitation class="brush: csharp; gutter: true;" width="650px"}

using System.Web.Mvc;

{/codecitation}

if it is not added

Step 4:

Inherit this class from Controller.

{codecitation class="brush: csharp; gutter: true;" width="650px"}

public class csharpController : Controller

{/codecitation}


Upto this step Controller is ready to add Actions.
Now this controller could be access as
http://localhost:1876/csharp

How to create a Action inside a Controller?


An action could get added in a controller by simply adding a public method inside controller class.
Whereas action having following restrictions
• The method must be public.
• The method cannot be a static method.
• The method cannot be an extension method.
• The method cannot be a constructor, getter, or setter.
• The method cannot have open generic types.
• The method is not a method of the controller base class.
• The method cannot contain ref or out parameters.


Happy Coding


About the Author


Dhananjay Kumar
Description :I done my engineering from Anand Engineering college Agra in 2007. I am MCTS WCF, MCTS MOSS Development, I am MCTS Web Development . I am native of Jamshedpur. Currently Please feel free to contact me regarding any clarification of my article at Dhananjay.25july@gmail.com

Occupation : Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dhananjay25


 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top