In this demo we will
be learning :
1. How to create a
controller
2. Displaying text on
screen
3. Overview of URL
redirecting in MVC for Contoller
Lets start by
launching VS for web, we will name this project as Demo2.
we will be making
use of “Internet Application” and “ASPX” as our view engine.
This will create
Demo2 project as below:
Now, we will be
adding a new Controller. For this, right click on Controller folder in Solution
Explorer and navigate to Add >> Controller and click on Controller this
will open Add Controller window.
Rename this
Contoller as EmployeeController.
Note : All controller
should be ending with name Controller. To know why read :
Do not select any
Template, we will learn how to make use of templates later. Just click Add.
This will add a new Controller inside Controller folder with name
EmployeeController.cs also VS will open the file up for you.
Explanation of
controller code :
Public ActionResult
Index()
{
}
This is a
declaration of method named Index(), which is declared inside a Controller,
more than one method can be declared inside a controller and any method which
is declared inside a controller can also be called action method.
Now, delete the ActionRestult method
in code and add below String method to the code.
public string HrDepartment()
{
return "Employee in HR
Department is Called";
}
public string PayrollDepartment()
{
return "Employee in Payroll Department is Called";
}
Page will look as
below :
Explanation of Code
:
public string HrDepartment()
{
return "Employee in HR
Department is Called";
}
Here HrDepartment()
is our ActionMethod in Controller
Employee & it will return a string.
Now lets run the
application, you will get default application screen. Now to access and get
values from the controller we need to change the URL in address bar of the
browser.
Let’s change it to :
So, now
to print the next set of output change URL to http://localhost:*****/Employee/PayrollDepartment
So, we are now able to display values on screen without using
View. Controller can be used to display text sentences on the screen. But, to
make a fullfledge web page design you require View.
Now lets understand how URL concept work. (URL redirections
plays a major part in MVC, There will be detailed chapter later on URL concept
and its working)
To understand this better now lets run our application and
redirect to http://localhost:*****/Employee this will give an Error page. This
is because we don’t have an Index() method declared in our Controller. Index
method works as a default method for a controller, and even when we don’t make
use of action name in URL it redirects to Index method if its declared in our
controller.
Declare a
Action Method Index() in our Employee Controller.
public string Index()
{
return "Index Method is
called, Index method works as an Default action for a controller";
}
Your
Employee controller will look as below:
Run the project and redirect to http://localhost:*****/Employee again. This time
MVC will rediect to Index actionmethod inside Employee Controller. Which shows
Index works as an default actionmethod for an Controller.
So now if you look at Home page URL its states only
localhost:*****, lets know how and where is this setup.
This page is coming from Home Controller and Index Action.
Even if you extend your URL to localhost:*****/Home/Index, it will take you to
the same page.
So, how is MVC handling this short URL, this is called URL
Rewriting. URL rewriting hides page name and other details from end user.
In MVC 4.0 it is done making use of Global.ascx.cs and
RouteConfig.cs files. In Solution Explorer you will find Global.ascx file, it
has its code behind file called Global.ascx.cs, when you open this file you
will find below lines in Application_Start() method. (Application start
method is the method which is called when application load)
Line No: 23
RouteConfig.RegisterRoutes(RouteTable.Routes);
Right Click on RegisterRoute and navigate to “Go to Definition”, it will take us to RouteConfig.cs which is
located inside folder App_Start, this is where our URL rewriting is done.
Now let’s understand the code in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action
= "Index", id = UrlParameter.Optional }
);
}
Line No 14:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
Above line will not allow user to get any file ending with
.axd in your Solution. As the name suggests IgnoreRoute suggests that no one
should be able to access any file with extension .axd, in the same way we can
hide other files like .pdf, .jpg, etc.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action
= "Index", id = UrlParameter.Optional }
);
Whenever a call is made by browser, a call is made to
MapRoute to check what redirects are in place.
Here, url: "{controller}/{action}/{id}", defines the sequence of invocation of the URL. I.e. in the
URL Controller should be called first, then action later to it id.
defaults:
new { controller = "Home", action
= "Index", id = UrlParameter.Optional },
This defines which default file to call and which
action to invoke. Here id is optional, because of which even when we are not
passing it in URL its working fine and taking us to Home/Index.
We will be learning creation of Views in next chapter
To learn more about MVC
follow all parts of this tutorial series.
For detailed documentation
and Video notes on MVC visit:
Created by:
Team: Techies@its4u
ITSolution4U – Technology
Expert
ITSolution4U Home Page:
http://www.itsolution4u.com