Routing in ASP.Net Dynamic Data

Ambily.raj
Posted by in ASP.NET category on for Beginner level | Points: 150 | Views : 25148 red flag
Rating: 5 out of 5  
 1 vote(s)

ASP.Net Dynamic Data is used to develop data driven web applications using dynamic page and field templates. In this article we will look into the custom routing mechanism supported by ASP.Net Dynamic Data.

Routing in ASP.Net Dynamic Data

ASP.Net Dynamic Data is used to develop data driven web applications using dynamic page and field templates. We discussed about the ASP.Net Dynamic Data in the first article. In this article we will look into the custom routing mechanism supported by ASP.Net Dynamic Data.

Routing

In web applications, the word ‘Routing’ means the process of defining a meaning URL or custom URL for the web pages.

For example, we have a URL, which edits the specified Order record. 
      http://localhost:48681/Orders/Edit.aspx?OrderID=10254

Instead of specifying Orders/Edit.aspx, we can specify a more meaningful URL say Edit/Order.aspx. In our ASP.Net Dynamic Data web application we have only one page Edit.aspx, which is used by all tables to do the editing operation. Even though we don’t have the Orders.aspx page, we can specify the above URL as http://localhost:48681/Edit/Orders.aspx?OrderID=10254 using Routing in ASP.Net Dynamic Data.

URL Routing

Open the Global.asax page code.

 
Default routing is added using the RegisterRoutes method in Global.asax file. Look into the following line, where the routing is defined. 

routes.Add(new DynamicDataRoute("{table}/{action}.aspx")

    {

        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),

        Model = DefaultModel

    });

  
This specifies that, for any action like List, details, edit or insert, generate the URL as table name/action.aspx. Edit operation in Product table will open the page with the URL Product/Edit.aspx.

You can form the URL according to your requirement. Few samples are

{action}/{table}

routes.Add(new DynamicDataRoute("{action}/{table}.aspx")

  {

    Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),

    Model = DefaultModel

  });

New URL: http://localhost:48681/Details/Products.aspx?ProductID=7

{table}_{action}

routes.Add(new DynamicDataRoute("{table}_{action}.aspx")

   {

     Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),

     Model = DefaultModel

   });

New URL:    http://localhost:48681/Products_Details.aspx?ProductID=6 
                 http://localhost:48681/Products_Edit.aspx?ProductID=6  

In-Line Editing using URL Routing

If you want to perform all operations like insert, update and delete of the records in the same page itself, use the different routing defined (commented) in the Global.asax file. If you look into the end of the Global.asax file, you can see the commented lines.




Comment the existing routing definition and uncomment the other two routing definitions. This will use the ListDetails page template for displaying the records. ListDetails template allows inline editing of the records. It also contains the detailed view of the selected record in the page.  We can insert records using the details view displayed in the bottom of the page.

Product Table using ListDetails template



Inline Editing of records

Insert, edit or delete records using the Form view given below the detailed view


Table-Specific Routing

Here, we will look into how we can use different routing for different tables.


For example, we need to do the edit and insert in a separate page only for the product table and for all other tables, we need to use the ListDetails template, which allows inline editing of records. This will be defined as follows.

 

public static void RegisterRoutes(RouteCollection routes)

  {

     // Register the Data Context.

     DefaultModel.RegisterContext(typeof(NorthWindDataContext), new ContextConfiguration() { ScaffoldAllTables = true });

 

     // Product table routing. Edit and Insert in Product table will be using separate page.

     routes.Add(new DynamicDataRoute("{action}/Products.aspx")

     {

        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),

        Model = DefaultModel,

        Table="Products"

     });

 

           

     // Inline Editing for all tables except Product table.           

routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")

     {

          Action = PageAction.List,

          ViewName = "ListDetails",

          Model = DefaultModel

     });

 

     routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")

      {

          Action = PageAction.Details,

          ViewName = "ListDetails",

          Model = DefaultModel

      });

 }


Product Table View



Product Table – Editing


Order Table View

Order Table – Editing


Conclusion

In ASP.Net dynamic data, we have the built-in support for routing. Using routing, we can define meaningful and useful URLs with dynamic page names. We will look into more customization in next article.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)