locked
Manual Scaffolding and Routing RRS feed

  • Question

  • User621401567 posted

     Hey all,

    I have two questions regarding the capabilites of Dynamic Data:

    1) I don't seem to be able to find any information on how to manually scaffold your data context if you would prefer not to scaffold all of your tables.

    I've tried playing with the API and I cant find it, Is there an article somewhere I can reference?

     2) I'm trying to play with my routes to try and form friendly urls. By friendly urls I mean replacing the out of the box Url: http://foo.com/MyTable/Edit.aspx?MyTableID=1 with a more Search engine friendly Url like: http://foo.com/MyTable/Edit/1

    Is this possible?

    Many Thanks

    Thursday, June 26, 2008 3:44 PM

Answers

  • User-330204900 posted

    What you do is set ScaffoldAllTables to false:

    model.RegisterContext(typeof(NWDataContext), new ContextConfiguration() { ScaffoldAllTables = false });

    And then:

    [ScaffoldTable(true)]
    public partial class Employee : INotifyPropertyChanging, INotifyPropertyChanged

    Scaffold each table with the ScaffoldTable attribute.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 26, 2008 4:09 PM
  • User-797310475 posted

    1. Or leave ScaffoldAllTables = true and use ScaffoldTable(false) to turn off scaffolding specific tables. I guess it depends on how many tables you want to scaffold and whether you prefere a whitelist or blacklist approach.

    2. You can use the following code in Global.asax. Just make sure it's before the default dynamic data route entry.

     

    routes.Add(new DynamicDataRoute("MyTable/{MyTableID}/Edit.aspx") {
      Action = PageAction.Edit,
      Model = model,
      Table = "MyTable"
    });
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 26, 2008 4:19 PM
  • User1641955678 posted

    I'm not sure that can be done easily.  What makes it hard is that some tables have multiple PK's, so that generic route wouldn't work well for them.  That being said, it's conceivable that something could be done by writing a custom derived route class which does some special processing of the URL.

    David

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 26, 2008 4:33 PM

All replies

  • User-330204900 posted

    What you do is set ScaffoldAllTables to false:

    model.RegisterContext(typeof(NWDataContext), new ContextConfiguration() { ScaffoldAllTables = false });

    And then:

    [ScaffoldTable(true)]
    public partial class Employee : INotifyPropertyChanging, INotifyPropertyChanged

    Scaffold each table with the ScaffoldTable attribute.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 26, 2008 4:09 PM
  • User933528419 posted

    1) I don't seem to be able to find any information on how to manually scaffold your data context if you would prefer not to scaffold all of your tables.

    I've tried playing with the API and I cant find it, Is there an article somewhere I can reference?

    Regarding question number 1 only.
    In the preliminary version of the docs located at codeplex, you can find a topic called "ASP.NET Dynamic Data Scaffolding and Page Templates Overview" in the file callled Preliminary ASP.NET Documentation.chm.
    There we explain how you can use the ScaffoldTableAttribute to select the tables you want to show when using the scaffolding mechanism. Is that what you're looking for?

    I hope this helps.

    Maíra

    Thursday, June 26, 2008 4:16 PM
  • User-330204900 posted

     2) I'm trying to play with my routes to try and form friendly urls. By friendly urls I mean replacing the out of the box Url: http://foo.com/MyTable/Edit.aspx?MyTableID=1 with a more Search engine friendly Url like: http://foo.com/MyTable/Edit/1

    See Scott Hunters blog post New Refresh of Dynamic Data Runtime and Tools is Posted and the section Dynamic Data Runtime third paragraph where is says Dynamic Data now can use routing not only for the path, but for the query string as well.

    Thursday, June 26, 2008 4:16 PM
  • User-797310475 posted

    1. Or leave ScaffoldAllTables = true and use ScaffoldTable(false) to turn off scaffolding specific tables. I guess it depends on how many tables you want to scaffold and whether you prefere a whitelist or blacklist approach.

    2. You can use the following code in Global.asax. Just make sure it's before the default dynamic data route entry.

     

    routes.Add(new DynamicDataRoute("MyTable/{MyTableID}/Edit.aspx") {
      Action = PageAction.Edit,
      Model = model,
      Table = "MyTable"
    });
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 26, 2008 4:19 PM
  • User1641955678 posted

    Steve answered #1.  For #2, try something like this:

                routes.Add(new DynamicDataRoute("MyProductDetails/{ProductID}") {
                    Model = model,
                    Table="Products",
                    Action="Details"
                });
    
      

    This route is specific to one table ("Products"), and needs to appear before the general route.

    thanks,
    David

    Thursday, June 26, 2008 4:20 PM
  • User621401567 posted

    Hey,

     Thanks to all for their responses.

    Regarding question 2: Does that mean I can only manage the ID parmaeter on a per table basis? Is there a way of simply defining a generic route that appends the Primary Key of any table to the end of a Route URL?

    Again, Thanks for your responses.

    Thursday, June 26, 2008 4:25 PM
  • User1641955678 posted

    I'm not sure that can be done easily.  What makes it hard is that some tables have multiple PK's, so that generic route wouldn't work well for them.  That being said, it's conceivable that something could be done by writing a custom derived route class which does some special processing of the URL.

    David

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 26, 2008 4:33 PM
  • User621401567 posted

    David, thats exactly the answer I'm looking for.

    Thanks for you help.

    Thursday, June 26, 2008 4:36 PM
  • User-797310475 posted

    Or you could just write a simple loop that creates all the routes for you automatically (this includes David's caveat about tables with multiple PKs, you'd have to handle that seperately):

    foreach (MetaTable table in model.Tables) {
      if (table.PrimaryKeyColumns.Count == 1) {
        string pkName = table.PrimaryKeyColumns[0].Name;
        string routeUrl = String.Format("{0}/Edit/{{{1}}}.aspx", table.Name, pkName);
        routes.Add(new DynamicDataRoute(routeUrl) {
            Model = model,
            Table = table.Name,
            Action = PageAction.Edit
        });
      }
    }
     
    Thursday, June 26, 2008 4:37 PM
  • User1641955678 posted
    Yes, Marcin's suggestion is simpler than trying to write a derived route class.  Note that it's a little less efficient, because you'll end up with many routes that the route engine needs to search sequentially.  But if you don't have a zillion tables that may be negligible.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><o:p> </o:p>
    Thursday, June 26, 2008 4:49 PM
  • User-330204900 posted

    As david said

    I'm not sure that can be done easily.  What makes it hard is that some tables have multiple PK's, so that generic route wouldn't work well for them.  That being said, it's conceivable that something could be done by writing a custom derived route class which does some special processing of the URL.

    You could write your own route handler to do something clever Mike Ormond has an article here Using ASP.NET Routing Independent of MVC and there's an other article Using ASP.NET Routing Independent of MVC - passing parameters to the page.

    Hope this helps

    Thursday, June 26, 2008 4:51 PM
  • User621401567 posted

     David, Marcin,

    Neither of your suggestions appear to work for me, I have tried several variations and have stripped out all other special routes that I have created.

    I am left with:

     

    1                foreach (MetaTable table in model.Tables)
    2                {
    3                    if (table.PrimaryKeyColumns.Count == 1)
    4                    {
    5                        string pkName = table.PrimaryKeyColumns[0].Name;
    6                        string routeUrl = String.Format("{0}/Edit/{{{1}}}.aspx", table.Name, pkName);
    7                        routes.Add(new DynamicDataRoute(routeUrl)
    8                        {
    9                            Model = model,
    10                           Table = table.Name,
    11                           Action = PageAction.Edit
    12                       });
    13                   }
    14               }
    15               routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
    16               {
    17                   Constraints = new RouteValueDictionary(new { action = "List|Details|Insert" }),
    18                   Model = model
    19               });
    20   
    
      Do I need yesterday's update to get this to work?
    Thursday, June 26, 2008 5:16 PM
  • User-797310475 posted

    Could you clarify how it does not work? It works for me, so we need more context to diagnose the problem.

    No, you don't need yesterday's update. You should have the 5-23a runtime update however, just in case.

    Thursday, June 26, 2008 5:26 PM
  • User621401567 posted

    Apologies - I'm normaly fairly thoughtful about what I need to write in a forum post!

    By it's not working I mean that the Edit Link has no Url/Href - it would appear that no Url is being generated for it at all.

    Thursday, June 26, 2008 5:51 PM
  • User1641955678 posted

    To isolate differences, could you try running the latest Futures, and adding the exact line from my previous mail (the one with MyProductDetails) before the generic route in global.asax.cs.  Then go to the Products list and click on one of the details link.  Doing exactly this works for me.

    thanks,
    David

    Thursday, June 26, 2008 6:03 PM
  • User621401567 posted

    David, Marcin,

    I will have to pick this up tommorrow - it's stupid o'clock over here in the UK and I've not left work yet.

    Thanks a lot for your help so far tonight.

    Thursday, June 26, 2008 6:12 PM
  • User-330204900 posted

    I've just tried what Marcin said and it worked for me, but I noticed that Details was left out (more important for search engines than Edit) so I did this:

    foreach (MetaTable table in model.Tables)
    {
    	if (table.PrimaryKeyColumns.Count == 1)
    	{
    		string pkName = table.PrimaryKeyColumns[0].Name;
    		string routeUrl = String.Format("{0}/{{action}}/{{{1}}}", table.Name, pkName);
    		routes.Add(new DynamicDataRoute(routeUrl)
    		{
    			Model = model,
    			Table = table.Name,
    			Action = PageAction.Edit + "|" + PageAction.Details
    		});
    	}
    }
    
    routes.Add(new DynamicDataRoute("{table}/{action}<STRIKE>.aspx</STRIKE>") //oops shoulda deleted this
    {
    	Constraints = new RouteValueDictionary(new { action = "List|Insert" }),
    	Model = model
    });

    and it worked [:D] I think I may be getting the hang of this routing now.

    Url's like this:

    http://localhost:16008/DD_NW/Orders/Details/10257
    Thursday, June 26, 2008 7:39 PM
  • User-330204900 posted

    And there's more:

    // adding embedded PK parameters to routing
    foreach (MetaTable table in model.Tables)
    {
    	string routeUrl = "";
    	string action = "";
    	switch (table.PrimaryKeyColumns.Count)
    	{
    		case 1:
    			string pkName = table.PrimaryKeyColumns[0].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}", table.Name, pkName);
    			action = PageAction.Edit + "|" + PageAction.Details;
    			break;
    		case 2:
    			string pkName1 = table.PrimaryKeyColumns[0].Name;
    			string pkName2 = table.PrimaryKeyColumns[1].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}/{{{2}}}", table.Name, pkName1, pkName2);
    			action = PageAction.Edit + "|" + PageAction.Details;
    			break;
    	}
    	
    	routes.Add(new DynamicDataRoute(routeUrl)
    	{
    		Model = model,
    		Table = table.Name,
    		Action = action
    	});
    }
    
    routes.Add(new DynamicDataRoute("{table}/{action}")
    {
    	Constraints = new RouteValueDictionary(new { action = "List|Insert" }),
    	Model = model
    });

    and of course you can keep going for more complex PK's [^o)] but like David said

    Yes, Marcin's suggestion is simpler than trying to write a derived route class.  Note that it's a little less efficient, because you'll end up with many routes that the route engine needs to search sequentially.  But if you don't have a zillion tables that may be negligible. 

    Sunday, June 29, 2008 8:27 PM
  • User-330204900 posted

    Sorry to go on, but (you know I'm not sorry realy, I am just trying to be polite) in ref to:

    Yes, Marcin's suggestion is simpler than trying to write a derived route class.  Note that it's a little less efficient, because you'll end up with many routes that the route engine needs to search sequentially.  But if you don't have a zillion tables that may be negligible. 

    You can reduce the number of routes created if for instance you know that there is a common route:

    that is always of the form {table}/{action}/{Id} i.e. the promary key is always Id, than you could do similar to below create your default route and then only add new routes if they do not follow the default route:

    // adding embedded PK parameters to routing
    // add default route with PKname = Id
    routes.Add(new DynamicDataRoute("{table}/{action}/{Id}")
    {
    	Action = PageAction.Edit + "|" + PageAction.Details,
    	Model = model
    });
    
    // add a route for each unique table PK combination NOT PKname = Id
    foreach (MetaTable table in model.Tables)
    {
    	
    	if (table.PrimaryKeyColumns.Count == 1 && table.PrimaryKeyColumns[0].Name == "Id")
    		continue; // if matched default route skip rest of loop
    	string routeUrl = "";
    	string action = "";
    	switch (table.PrimaryKeyColumns.Count)
    	{
    		case 1:
    			string pkName = table.PrimaryKeyColumns[0].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}", table.Name, pkName);
    			action = PageAction.Edit + "|" + PageAction.Details;
    			break;
    		case 2:
    			string pkName1 = table.PrimaryKeyColumns[0].Name;
    			string pkName2 = table.PrimaryKeyColumns[1].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}/{{{2}}}", table.Name, pkName1, pkName2);
    			action = PageAction.Edit + "|" + PageAction.Details;
    			break;
    	}
    
    	routes.Add(new DynamicDataRoute(routeUrl)
    	{
    		Model = model,
    		Table = table.Name,
    		Action = action
    	});
    }
    
    routes.Add(new DynamicDataRoute("{table}/{action}")
    {
    	Action = PageAction.List + "|" + PageAction.Insert,
    	Model = model
    });

    In my case most of my table end up with Id as the PK with the foreign key following the pattern of <TableName>Id.

    Monday, June 30, 2008 4:36 AM
  • User1641955678 posted

    Steve, I don't think you can write 'Action = PageAction.List + "|" + PageAction.Insert'.  Seatting this Action gives a default action if non comes from the URL, which will never happen here.  It's useful for cases where the path does not include {action}, but then it must be a single value.

    Here, you can either remove the Action line altogether, or replace it by the Constraints line if you don't want the route to match unknown actions.  Removing the Contraints is probably the simplest.

    David

    Monday, June 30, 2008 9:46 AM
  • User-330204900 posted

    Hi David, well it seems to work for me, when I debug the correct values are in the action, hope I've not broken something [:$]

    Monday, June 30, 2008 10:03 AM
  • User1641955678 posted

    For it to 'work' would mean that if you try to enter a URL with an Action that doesn't have one of the set values it will not match it.  With Constraint you get this.  With Action as you have it, I think it is basically a no op.  It doesn't break anything, but doesn't do anything either :).  Try removing your Action line to see if it makes any difference.

    David

    Monday, June 30, 2008 10:08 AM
  • User-330204900 posted

    OK David I'll give that a try and I will send you an e-mail with screen shots of the debug of the route [:D]

    Monday, June 30, 2008 11:02 AM
  • User1641955678 posted

    I think it should be possible to write a single Route that does everything, but that will require using a derived Route class.  The general idea is to use fixed tokens (say PK1, PK2, ...) to represent the primary keys, and then switch them for the real PK names in the custom route.  This way you could write something like "{table}/{action}/{PK1}", and it will work with all tables that have one PK, no matter what that PK is called.

    If I get some time, I'll play around with trying this.

    David

    Monday, June 30, 2008 11:05 AM
  • User1641955678 posted

    Ok, here it goes.  First, the custom route class:

        public class PKDynamicDataRoute : DynamicDataRoute {
    
            public PKDynamicDataRoute(string url): base(url) {
            }
    
            public override RouteData GetRouteData(HttpContextBase httpContext) {
                RouteData routeData = base.GetRouteData(httpContext);
    
                if (routeData != null) {
                    // Change any reference to PK1, PK2, etc into the actual PK name
                    FixUpRouteValues(routeData.Values, true /*fromFixedName*/);
                }
    
                return routeData;
            }
    
            public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
    
                // Change any reference to the actual PK name into PK1, PK2, etc...
                FixUpRouteValues(values, false /*fromFixedName*/);
    
                return base.GetVirtualPath(requestContext, values);
            }
    
            private void FixUpRouteValues(RouteValueDictionary routeValues, bool fromFixedName) {
    
                // First, try to get the table from the route values
                object tableName;
                if (!routeValues.TryGetValue("table", out tableName))
                    return;
    
                MetaTable table;
                if (!Model.TryGetTable((string)tableName, out table))
                    return;
    
                // Go through all the table's PK and make the required adjustment
                int index = 1;
                foreach (MetaColumn column in table.PrimaryKeyColumns) {
    
                    // Assign from and to based on which direction we're fixing up
                    string from, to;
                    if (fromFixedName) {
                        from = "PK" + index;
                        to = column.Name;
                    }
                    else {
                        from = column.Name;
                        to = "PK" + index;
                    }
    
                    // If the route has that value, fix it up
                    object val;
                    if (routeValues.TryGetValue(from, out val)) {
                        routeValues[to] = val;
                        routeValues.Remove(from);
                    }
    
                    index++;
                }
            }
        }
    
      

    And then to use it, try something like this:

                routes.Add(new PKDynamicDataRoute("{table}/{action}/{PK1}/{PK2}") {
                    Model = model,
                    Defaults = new RouteValueDictionary(new { PK2 = "" })
                });
    
     

    Maybe I'll add this to the Futures solution as it demonstrates the use of a custom route.

    thanks,
    David

    Monday, June 30, 2008 1:36 PM
  • User-330204900 posted

    For it to 'work' would mean that if you try to enter a URL with an Action that doesn't have one of the set values it will not match it.  With Constraint you get this.  With Action as you have it, I think it is basically a no op.  It doesn't break anything, but doesn't do anything either :).  Try removing your Action line to see if it makes any difference.

    OK David I removed all Action's an Constraints and everything still works, is there any where I can read up on what they actually do?

    // adding embedded PK parameters to routing
    // add default route with PKname = Id
    routes.Add(new DynamicDataRoute("{table}/{action}/{Id}")
    {
    	Model = model
    });
    
    // add a route for each unique table PK combination NOT PKname = Id
    foreach (MetaTable table in model.Tables)
    {
    
    	if (table.PrimaryKeyColumns.Count == 1 && table.PrimaryKeyColumns[0].Name == "Id")
    		continue; // if one PK and name = Id continue
    	string routeUrl = "";
    	switch (table.PrimaryKeyColumns.Count)
    	{
    		case 1:
    			string pkName = table.PrimaryKeyColumns[0].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}", table.Name, pkName);
    			break;
    		case 2:
    			string pkName1 = table.PrimaryKeyColumns[0].Name;
    			string pkName2 = table.PrimaryKeyColumns[1].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}/{{{2}}}", table.Name, pkName1, pkName2);
    			break;
    	}
    
    	routes.Add(new DynamicDataRoute(routeUrl)
    	{
    		Model = model,
    		Table = table.Name
    	});
    }
    
    routes.Add(new DynamicDataRoute("{table}/{action}")
    {
    	Model = model
    });
    

    The above still works single and dual PK's, List Details, Inserts and Edit [:|] I'm puzled as to what Actions and Constraints do.

    Monday, June 30, 2008 2:56 PM
  • User1641955678 posted

    Yes, I know that can be confusing, and the current docs may not have complete details on this.  The quick summary is:

    • Setting Action sets the action to be used when the path doesn't contain {action}.  e.g. see the ListDetails.aspx routes in global.asax (commented out by default)
    • Setting Contraints limit the set of acceptable value for a variable like {action}.  So unlike Action, it only makes sense to have it when you do have {action} in the path.

    Hopefully this clarifies a bit :)

    David

    Monday, June 30, 2008 3:55 PM
  • User-330204900 posted

    Thanks David, that explains what they both do, but what confuses me is why does my code work without either?

    Is it because each of my routes has an {action} in it?

    Monday, June 30, 2008 4:03 PM
  • User1641955678 posted

    Well, since the path has {action}, setting Action is not expected to do anything, which is why removing it has no effect.  As for Constraints, removing it does have an effect: it allows the route to be used for any action, instead of just the specified set.  But since no action other than those would typically be attempted in the standard scaffolded app, you would effectively not see a difference.

    Whether you want to have the Constraints or not depends on the scenario.  The Contraint can potentially avoid getting the wrong route matched, but in practice that's not likely, so you're fine without it.

    David

    Monday, June 30, 2008 4:10 PM
  • User-330204900 posted

    Thanks for your PKDynamicDataRoute class it works a treat [:D]

    Monday, June 30, 2008 6:00 PM
  • User1641955678 posted

    Great!  Let me know if you find any issues, as I didn't have a chance to test it much.  If it holds up I'll put it in the next Futures build.

    David

    Monday, June 30, 2008 6:10 PM
  • User-330204900 posted

    Well, since the path has {action}, setting Action is not expected to do anything, which is why removing it has no effect.  As for Constraints, removing it does have an effect: it allows the route to be used for any action, instead of just the specified set.  But since no action other than those would typically be attempted in the standard scaffolded app, you would effectively not see a difference.

    Whether you want to have the Constraints or not depends on the scenario.  The Contraint can potentially avoid getting the wrong route matched, but in practice that's not likely, so you're fine without it.

    David

    Right [:$] now I understand, I feel so dumb. Constraints come into their own when you have complex routes and you don't want things to get mixed up.

    In my test site if I have not Constraints then this URL works http://localhost:8423/DD_Routes/AttributesColumns/List/Categories/CategoryID where clearly these elements of the URL /Categories/CategoryID are not valid for a List page so by adding a Constraint I now get a "The resource cannot be found" error on the page which means the Constraint is now working stopping routes with an invalid action will NOT be matched by this route any longer.

    My finished code (no longer relavent because of Davids derived routing class earlier in this thread:

    // adding embedded PK parameters to routing
    // add default route with PKname = Id
    string actions = "Edit|Details";
    routes.Add(new DynamicDataRoute("{table}/{action}/{Id}")
    {
    	Constraints = new RouteValueDictionary(new { action = actions }),
    	Model = model
    });
    
    // add a route for each unique table PK combination NOT PKname = Id
    foreach (MetaTable table in model.Tables)
    {
    
    	if (table.PrimaryKeyColumns.Count == 1 && table.PrimaryKeyColumns[0].Name == "Id")
    		continue; // if one PK and name = Id continue
    	string routeUrl = "";
    	switch (table.PrimaryKeyColumns.Count)
    	{
    		case 1:
    			string pkName = table.PrimaryKeyColumns[0].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}", table.Name, pkName);
    			break;
    		case 2:
    			string pkName1 = table.PrimaryKeyColumns[0].Name;
    			string pkName2 = table.PrimaryKeyColumns[1].Name;
    			routeUrl = String.Format("{0}/{{action}}/{{{1}}}/{{{2}}}", table.Name, pkName1, pkName2);
    			break;
    	}
    
    	routes.Add(new DynamicDataRoute(routeUrl)
    	{
    		Model = model,
    		Table = table.Name,
    		Constraints = new RouteValueDictionary(new { action = actions })
    	});
    }
    
    routes.Add(new DynamicDataRoute("{table}/{action}")
    {
    	Constraints = new RouteValueDictionary(new { action = "List|Insert" }),
    	Model = model
    });

    I have posted this JUST to correct my earlier posts.

    [:D] my head has stopped spinning now [:S]

    Tuesday, July 1, 2008 3:12 AM
  • User570516596 posted

    Steve,

    I implemented the URL Routing for first level and trying to implement the same for multi levels. below url is my test site http://localhost:1706/URLRouting/Equity where "Equity is the Routing Name" for the first level. Now under "Equity" am having multiple pages so want to implement url routing for the same, Here are the details about my implementation 1. PageDetails table contains page id, routing name and page url. Based on the routing name entered in the browser, am filtering the Page Details table to get the page url and the this virttual path is being passed to build manager to create instance and below is the code written in custom route handler.

    public IHttpHandler GetHttpHandler(RequestContext requestContext)

    {

    string pageName

    = requestContext.RouteData.GetRequiredString("Parameter").Trim();

    //pageName =

    // pageName.ToLower() == "home" ? "default" : pageName;

    DataRow dr = GetPageDetails(pageName);

    string virtualPath = dr["PageUrl"].ToString();

    requestContext.HttpContext.Items["PageID"] = dr["PageID"].ToString();return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));

    }

    Iam getting confused by seing all the posts about the url routing whether i need to change my implementation as per the posts here.

    Saturday, September 20, 2008 4:19 AM
  • User570516596 posted

    Implemented URL routing using the links provided (Using ASP.NET Routing Independent of MVC - passing parameters to the page.), it was wokring superb, but when i ajax toolkit control to the website am getting error "ASP.NET Ajax client-side framework failed to load", and while looping through the Route data am getting some resource file names instead actual pagename(route name). Is there any solution for the same..?i tried using Routes.IgnoreRoute also, but no use. Below is the sample code for the same.

    Global.asax  

    Global.asax
    
    void Application_Start(object sender, EventArgs e) 
    
        {
    
            // Code that runs on application startup
    
            RegisterRoutes(RouteTable.Routes);
    
        }
    
     
    
        private void RegisterRoutes(RouteCollection Routes)
    
        {
    
            Route r1 = new Route("{First}", new MEOSimpleRouteHandler());
    
            Routes.Add(r1);
    
     
    
            // Handler to stop URL routing for Web resources.
    
            Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            Routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
    
            Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
    
        }
    
     
    
    Custom Route Handler:
    
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    
        {
    
            string pageName
    
                = requestContext.RouteData.GetRequiredString("First").Trim();
    
     
    
    //Get page data source and filter the same based on the page name.
    
            DataView dv = new DataView(getUrlRoutingSource());
    
            dv.RowFilter = "RoutingName = '" + pageName + "'";
    
     
    
            DataRow dr = dv[0].Row;
    
     
    
            string virtualPath = dr["PageUrl"].ToString();
    
            HttpContext.Current.Items["PageID"] = dr["PageID"].ToString();
    
            return (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));
    
        }
    
     
    
    private DataTable getUrlRoutingSource()
    
        {
    
            DataTable dt = new DataTable();
    
            dt.Columns.Add("RoutingName");
    
            dt.Columns.Add("PageUrl");
    
            dt.Columns.Add("PageID");
    
            dt.Columns.Add("ParentPageID");
    
     
    
            DataRow dr = dt.NewRow();
    
            dr["RoutingName"] = "Equity";
    
            dr["PageUrl"] = "~/Pages/Default.aspx";
    
            dr["PageID"] = "1";
    
            dr["ParentPageID"] = null;
    
            dt.Rows.Add(dr);
    
     
    
            dr = dt.NewRow();
    
            dr["RoutingName"] = "Mutual Funds";
    
            dr["PageUrl"] = "~/Pages/Default.aspx";
    
            dr["PageID"] = "2";
    
            dr["ParentPageID"] = null;
    
            dt.Rows.Add(dr);
    
    return dt;
    
    }

    Mohan...

    Wednesday, September 24, 2008 8:59 AM
  • User570516596 posted

    Mistake from my side, forgot to put IgnoreRoute on top before adding routes.

    Friday, September 26, 2008 1:44 AM