Controllers and routers in ASP 3
AMBILYKK, 3 May Cpol
4.79 (Votes)
Rate:
Vote 1vote 2vote 3vote 4vote 5
A deeper look into the pillars of ASP. Mvc–routers and Controllers. Introduction
ASP. NET MVC provides a new-creating Web applications which is more extensible and testable. We discussed about ASP. NET MVC in Introduction to ASP. 3. Here, we'll have a deeper look into the pillars of ASP. Mvc–routers and Controllers. Routers
One of the main objectives of ASP. NET MVC is Search Engine optimization (SEO). Search Engines work mainly using URLs. Defining a meaningful and more understandable URLs are very important to make we application more search engine friendly.
Routing is the "the" constructing meaningful URLs for a Web request. As you had already seen, our MVC application URLs is not represented by extensions like . aspx. Instead, the URLs consist of the Controller name and Action name.
Let us first understand how the default routing works. Open the global.ascx file, and we can see the Application_Start()
and RegisterRoute()
methods.
Collapse | Copy Code
public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. MapRoute ( "Default",//Route name "{controller}/{action}/{id}",//URL with parameters New {controller = "H ome ", action =" Index ", id = urlparameter.optional} //Parameter defaults );}
Look at the statement where we map the routing. Our URL formation uses the pattern "{controller}/{action}/{id}", where ID was an optional parameter.
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Specifies that is the URL does not specify a controller and use the Home controller. Also, in the absence of an action, it uses the Index action, and the last parameter is optional.routing data inside a Cont Roller
We can access routing data inside a Controller using the RouteData
object.
Collapse | Copy Code
Public ActionResult Index () { viewbag.message = string. Format ("{0}---{1}--{2}", routedata.values["Controller"], routedata.values["action"], routedata.values["id"] ); return View ();}
Controllers
Now let us create a new controller and see how we can route to the new controller using a different routing pattern.
Add a new controller using Add new Item--controller. It adds a new Controller with an Action as Index. For our sample application, we are using a different Action called Verify
.
Collapse | Copy Code
public class samplecontroller:controller{ /// GET:/sample/public actionresult Verify () { return View ();} }
As there is no views corresponding SampleController
to, let us return some the text from our Action. For returning any text data from a Action, use the Content
class.
Collapse | Copy Code
Public ActionResult Verify () { return Content ("Hello from Sample Controller.");
Let us run the application. Modify the URL to /sample/verify.
But if we specify /sample without any Action, we'll receive a 404 error. As per the defined routing, if there is no Action specified, it should redirect to the Index Action inside the specified C Ontroller. Here, our SampleController
doesn ' t has any Index action and throws an error.
Adding a new route
For fixing the above issue, let us define a new route called " sample
".
Collapse | Copy Code
public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); routes. MapRoute ( "Sample", "Sample/{action}", new {controller = "sample", action = "Verify"} ); This must be placed in front of the default, otherwise you will not find routes. MapRoute ( "Default",//Route name "{controller}/{action}/{id}",//URL with parameters New {controller = "Home", action = "Index", id = urlparameter.optional}//Parameter defaults );}
Now we could need to pass some data to our new Controller from a URL, like the id
parameter in the default routing. For this, define a new parameter in the routing.
Collapse | Copy Code
Routes. MapRoute ( "Sample", "Sample/{username}", new {controller = "sample", action = "Verify"} );
The value can accessed either using the RouteData
object or through a parameter to the Verify
action.
Collapse | Copy Code
Public ActionResult Verify (string username) { return Content (username);}
Note that the URL is consists of only the Controller and the parameter.
Again, you'll receive a 404 error when we omit the parameter value.
For solving this issue, we need-specify the default value for the username parameter either in the Route mapping or in The Controller.
In the route map, specify the parameter as optional.
Collapse | Copy Code
Routes. MapRoute ( "Sample", "Sample/{username}", new {controller = "sample", action = "Verify", username= Urlparameter.optional} );
Inside the Controller, specify the default value for the parameter.
Collapse | Copy Code
Public ActionResult Verify (string username= "all") { return Content (username);}
Conclusion
We had a quick discussion on what routing works in ASP. NET MVC and how we can customize the same. We'll discuss more on views, Styles, Action results, etc., in the next article.
"Go" Controllers and routers in ASP 3