Saltar apartados

Routing

The enrutamiento ASP.NET Allow to use directions URL that is not necessary to assign to specific files of a website. Since the direction URL does not have to assign to a file, can use directions URL that describe the action of the user and, therefore, are easier to comprise.

In an application ASP.NET That it does not use the enrutamiento, an application entrante of a direction URL usually assign to a physical file that controls the application, as a file .aspx. For example, an application of http://server/application/Products.aspx?Go=4 assign to a file designated Products.aspx That contains code and marked to represent an answer to the navigator. The page web uses the value of chain of query go=4 to determine the type of content that goes to show.

In the enrutamiento of ASP.NET, can define models of direction URL that assign to files of controller of applications but that no necessarily include the names of these files in the direction URL. Besides, can include marcadores of position in a model of direction URL so that can happen variable data to the controller of applications without need of a chain of query.

The models of direction URL for the routes in the applications of MVC are used to to include the marcadores of position {controller} and {action}.

For example, a direction URL that includes the route of access /Products is assigned to a controller designated ProductsController. The value of the parameter action is the name of the method of action that invoke . A direction URL that includes the route of access /Products/show would give place to a call to the method Show of the class ProductsController.

The Model of direction URL predetermined is the following

{controller}/{action}/{Go}

And a direction that correspond with this enrutamiento would be

Http://server/Catalog/Listed/courses

  • Controller: I catalog
  • Action: Listing
  • Go: courses

Add routes to an application of MVC

It is possible to create our own implementation of controllers with classes that derive of the class ControllerBase. This process is something painstaking with what go us to centre the enrutamiento that defines the global file.asax.

To continuation defines the enrutamiento by defect that generates the staff of project of Visual Studio for the applications of MVC.

public class MvcApplication : System.Web.HttpApplication

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

"Default", // Route name

"{controller}/{action}/{Go}", // URL with parameters

new { controller = "Home", action = "Index", go = "" } // Parameter defaults

);

}

protected void Application_Start()

{

RegisterRoutes(RouteTable.Routes);

}

}

In the section MapRoute can see that you define with the previously commented format. In the following line defines the values by defect.

If we wanted to add a third parameter to the route that indicated the language of the application, would be so simple like adding in the section MapRoute the parameter {language}. In the third session will see like doing use of the parameter and can create applications multiidiomáticas.

public class MvcApplication : System.Web.HttpApplication

{

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(

"Default", // Route name

"{language}/{controller}/{action}/{Go}", // URL with parameters

new { language = "Is", controller = "Catalogador", action = "Index", go = UrlParameter.Optional } // Parameter defaults

);

}

protected void Application_Start()

{

AreaRegistration.RegisterAllAreas();

RegisterRoutes(RouteTable.Routes);

}

    }

Computing service ASP.NET MVC 2 Framework


Universidad de Alicante
Carretera de San Vicente del Raspeig s/n
03690 San Vicente del Raspeig
Alicante (Spain)

Tel: (+34) 96 590 3400

Fax: (+34) 96 590 3464

For further information: informacio@ua.es, and for enquiries on this web server: webmaster@ua.es

Carretera San Vicente del Raspeig s/n - 03690 San Vicente del Raspeig - Alicante - Tel. 96 590 3400 - Fax 96 590 3464