Saturday, 17 November 2012

What ASP.NET MVC Route table ?


 ASP.NET MVC application. MVC is application development architure. MVC represents ---Model, View,Controller.
Simply--à Model-àrepresent a type. View-à User Interface, Controller--à User Actions.
Controller:
Controller is the responsable for handling user requests. Very Controller is coupled/tied with a View.
A controller can have more then one action. We need to specify the which action to perform we have specify as REST style. Based on type action request will be processed and their associated view will be returned to client.

EX:


Bydefault Route is prepared for Home(Controller) and Index(Action). If we run the asp.net mvc application  which is executed as per our Route (Route: RouteName,Controller,Action,parameters).

Action :

http://localhost:3866/Home/Index(default)

so  we need to specify which controller as well which action.
Parameters:

We can optionally specify the Parameters also.
Finally a RouteTable will be prepared as per our Requested URI


Demo:

HomeController
public ActionResult MyAction(int id)
{

            ViewData["MyValue"] = id;
            return View();---------------MyAction.aspx(view)
       
}
    MyAction.aspx

  <h2>MyAction</h2>

 Entered Value is:<%: ViewData["MyValue"] %>




How Route is prepared ?

yes, as per our question  For Every Request Based  Route table will be Prepared by RoutTable

Then specify the Route as

public ActionResult MyAction(int id)
{

            ViewData["MyValue"] = id;
            return View();---------------MyAction.aspx(view)
       
}


then Route in Global.asax

public static void RegisterRoutes(RouteCollection routes)
{
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
           new { controller = "Home", action = "MyAction", id = UrlParameter.Optional }                         );

}






Here I specifies the Route is à Home(controller), action(MyAction),id(optional).

But MyAction requires --à a Parameter





No comments:

Post a Comment