Manejo de rutas en sitios web Dinámicos con ASP.NET

18 12 2008

Hola, siguiendo con el anterior ejemplo, podemos cambiar la ruta de nuestras páginas web mediante acciones.

Veamos el global.asax completo, antes de iniciar con las modificaciones.

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.DynamicData" %>

<script RunAt="server">
    public static void RegisterRoutes(RouteCollection routes) {
        MetaModel model = new MetaModel();

        //                    IMPORTANT: DATA MODEL REGISTRATION 
        // Uncomment this line to register LINQ to SQL classes or an ADO.NET Entity Data
        // model for ASP.NET Dynamic Data. Set ScaffoldAllTables = true only if you are sure 
        // that you want all tables in the data model to support a scaffold (i.e. templates) 
        // view. To control scaffolding for individual tables, create a partial class for 
        // the table and apply the [Scaffold(true)] attribute to the partial class.
        // Note: Make sure that you change "YourDataContextType" to the name of the data context
        // class in your application.
        //model.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() { ScaffoldAllTables = false });

        // The following statement supports separate-page mode, where the List, Detail, Insert, and 
        // Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
        // route definition, and comment out the route definitions in the combined-page mode section that follows.
        routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
            Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
            Model = model
        });

        // The following statements support combined-page mode, where the List, Detail, Insert, and
        // Update tasks are performed by using the same page. To enable this mode, uncomment the
        // following routes and comment out the route definition in the separate-page mode section above.
        //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
        //    Action = PageAction.List,
        //    ViewName = "ListDetails",
        //    Model = model
        //});

        //routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
        //    Action = PageAction.Details,
        //    ViewName = "ListDetails",
        //    Model = model
        //});
    }

    void Application_Start(object sender, EventArgs e) {
        RegisterRoutes(RouteTable.Routes);
    }

</script>
Como lo hemos hecho al principio hemos colocado nuestro contexto de datos.
model.RegisterContext(typeof(myDataContextDataContext),
          new ContextConfiguration() { ScaffoldAllTables = true });
Si checamos bien tenemos dentro de la carpeta de DynamicData, tenemos las plantillas, como puedes ver en la imagen.
 
routingwithDynamicDataWebSite01
Estas plantillas, nos van a servir y las vamos a tomar como acciones, dentro de nuestra base de datos dependiendo de la tabla que eligamos al principio.
 
Por ejemplo vamos a quitar el comentario de la parte de Routes (rutas), y en esta parte estamos indicando de que cuando presionemos que queramos ver la lista (List) de elementos según la tabla, nos redireccione a la plantilla ListDetails, o cualquiera que nosotros personalizemos.
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
         Action = PageAction.List,
         ViewName = "ListDetails",
        Model = model
     });
Y si quitamos los siguientes comentarios, tendremos
Este código indica que si queremos ver los detalles de los elementos de la tabla escogida, entonces nos redireccione igual a la plantilla ListDetails.
routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
           {
           Action = PageAction.Details,
           ViewName = "ListDetails",
           Model = model
       });

Código completo:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.DynamicData" %>

<script RunAt="server">
    public static void RegisterRoutes(RouteCollection routes) {
        MetaModel model = new MetaModel();

        model.RegisterContext(typeof(myDataContextDataContext),
            new ContextConfiguration() { ScaffoldAllTables = true });

            routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx") {
            Action = PageAction.List,
            ViewName = "ListDetails",
           Model = model
        });

            routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
            {
            Action = PageAction.Details,
            ViewName = "ListDetails",
            Model = model
        });
    }

    void Application_Start(object sender, EventArgs e) {
        RegisterRoutes(RouteTable.Routes);
    }

</script>
Vemos en pantalla:
routingwithDynamicDataWebSite

La gran ventaja de esto, es que el ejemplo anterior ocupabamos una página o plantilla para editar, para lista, para agregar elementos y en esta plantilla ListDetails, podemos hacerlo todo junto y la ruta  no va a cambiar.

routingwithDynamicDataWebSite02

Saludos.


Actions

Information

Leave a comment