Thank you Andrea, that works although it does not appear to make any sense. Why is the string I am testing for related to the ultimate destination of my route?
What if I want to do this: routes.Add(new DynamicDataRoute("{slug}/Articles") and route to Authors.aspx?
I'm not trying to be difficult I just want to understand how this works.[:)]
>>You could also move the other elements of the URL about and it will still work
{Id}/{table}/{action},
the order you put these URL parameters is up to you, what ever works for your application.
Where in the website directory structure would you put the page for such a route?
My root question is why cant the route that tests for "Orders/ {id}" map to whatever the page specified by the action is (i.e. someDirectory/SomePage.aspx vs. Orders/SomePage.aspx).
Where in the website directory structure would you put the page for such a route?
Same as normal you don't need to change the folder. it's the pattern that matters.
As for your Orders/{id} if you have a folder that in not under the ~/DyanmicData folder then you would not need a route however if it is under the ~/DynamicData folder the it must match a route and the order that they are declared matters so if Orders/{id}
matches a table Orders then that match will work first.
Dynamic Datarouting
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
You say: Same as normal you don't need to change the folder. it's the pattern that matters.
So why do I need to move my ArticleViewer page to /Articles/ArticleViewer.aspx?
Do you mean it's the table that matters?
The first route works however it appears to use the name of the table (vs. "SomeString") to locate the .aspx page.
If I place ArticleViewer in a directory called SomeString the route does not work.
The second route does not work even if the directory SomeString is placed under Articles.
In the action parameter of the route why cant I say Action="/somewhere/ArticleViewer.aspx"?
Is it mandatory to place the action page in a directory whos name corresponds with a table?
OK I'll try to explain. the reason you have to move the file is if you create a custom page that is to be used with a particular table then the correct behavoir is to move it to the ~/DynamicData/CustomPage/TableName/Page.aspx this is the
default behavoir. DD routes always translate to the ~/DynamicData folder if there is a CustomePages folder for that table it will look there fist for the relavent page if not it will use a page from the PageTemplates folder.
OK so far.
Now if you have add a new route into the equasion it's still going to look at thing in the above way unless you make it clear in your route that the file is outside the ~/DynamicData folder.
So if I had a page ~/Articles.aspx (in the root) of the site I would not need to provide a route to acces this file unless I wanted to have the paramter slug passed in as part of the url:
~/Articles.aspx?slug=10
So if I wanted to have a url like:
~/Articles.aspx/10
or
~/Articles/10
the I could add my own route handler, because if you look at a route:
routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = model
});
samw
Member
180 Points
264 Posts
Routing question
Apr 02, 2009 04:49 AM|LINK
I have a .aspx page in /DynamicData/CustomPages called ArticleViewer.aspx
Global.asax looks like this:
routes.Add(new DynamicDataRoute("Articles / {slug}") { Action="ArticleViewer" }); routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = model });I want my url to look like this: http://mysite/articles/somearticle
I only get 404 - what am I doing wrong?
Thanks
andreadottor
Contributor
2060 Points
312 Posts
MVP
Re: Routing question
Apr 02, 2009 07:21 AM|LINK
Try with this:
routes.Add(new DynamicDataRoute("Articles/{slug}") { Action="ArticleViewer", Model = model, Table = "Articles" });but I not sure that you can't retrieve the {slug} value.
[UPDATE: ]
for retrieve slug, you can try to use this code in the page:
RequestContext requestContext = DynamicDataRouteHandler.GetRequestContext(HttpContext.Current);
string slug = requestContext.RouteData.GetRequiredString("slug");
Andrea Dottor
Microsoft MVP - ASP/ASP.NET
http://blog.dottor.net
samw
Member
180 Points
264 Posts
Re: Routing question
Apr 02, 2009 03:21 PM|LINK
Thank you Andrea,
I tried what you said but I still get 404.
Route in Global.asax:
routes.Add(new DynamicDataRoute("Articles/{slug}")
{
Action = "ArticleViewer",
Model = model,
Table = "Articles"
});
It is the first route, ScaffoldAllTables = true
ArticleViewer.aspx exists in /DynamicData/CustomPages
URL I put in my browser: http://localhost:1549/Articles/xyz
Thanks & Regards,
Sam
andreadottor
Contributor
2060 Points
312 Posts
MVP
Re: Routing question
Apr 02, 2009 03:48 PM|LINK
Put the page in a subfolder
/DynamicData/CustomPages/Articles/ArticleViewer.aspx
Andrea Dottor
Microsoft MVP - ASP/ASP.NET
http://blog.dottor.net
samw
Member
180 Points
264 Posts
Re: Routing question
Apr 02, 2009 04:46 PM|LINK
Thank you Andrea, that works although it does not appear to make any sense. Why is the string I am testing for related to the ultimate destination of my route?
What if I want to do this: routes.Add(new DynamicDataRoute("{slug}/Articles") and route to Authors.aspx?
I'm not trying to be difficult I just want to understand how this works.[:)]
Thanks very much!
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Routing question
Apr 02, 2009 05:25 PM|LINK
Hi Samw, have a look at this article of mine here Dynamic Data and Routes (Take 2) it give a very basic intro to routes.
Dynamic Data routing
Always seeking an elegant solution.
samw
Member
180 Points
264 Posts
Re: Routing question
Apr 02, 2009 07:20 PM|LINK
Hi Steve, in your article you say:
>>You could also move the other elements of the URL about and it will still work {Id}/{table}/{action}, the order you put these URL parameters is up to you, what ever works for your application.
Where in the website directory structure would you put the page for such a route?
My root question is why cant the route that tests for "Orders/ {id}" map to whatever the page specified by the action is (i.e. someDirectory/SomePage.aspx vs. Orders/SomePage.aspx).
Thanks
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Routing question
Apr 02, 2009 08:20 PM|LINK
Same as normal you don't need to change the folder. it's the pattern that matters.
As for your Orders/{id} if you have a folder that in not under the ~/DyanmicData folder then you would not need a route however if it is under the ~/DynamicData folder the it must match a route and the order that they are declared matters so if Orders/{id} matches a table Orders then that match will work first.
Dynamic Data routing
Always seeking an elegant solution.
samw
Member
180 Points
264 Posts
Re: Routing question
Apr 02, 2009 10:34 PM|LINK
I'm sorry I'm so confused about this...
You say: Same as normal you don't need to change the folder. it's the pattern that matters.
So why do I need to move my ArticleViewer page to /Articles/ArticleViewer.aspx?
Do you mean it's the table that matters?
Look at these routes:
routes.Add(new DynamicDataRoute("SomeString/{slug}")
{
Action = "ArticleViewer",
Model = model,
Table = "Articles"
});
routes.Add(new DynamicDataRoute("Articles/{slug}")
{
Action = "SomeString/ArticleViewer.aspx",
Model = model,
Table = "Articles"
});
The first route works however it appears to use the name of the table (vs. "SomeString") to locate the .aspx page.
If I place ArticleViewer in a directory called SomeString the route does not work.
The second route does not work even if the directory SomeString is placed under Articles.
In the action parameter of the route why cant I say Action="/somewhere/ArticleViewer.aspx"?
Is it mandatory to place the action page in a directory whos name corresponds with a table?
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Routing question
Apr 03, 2009 09:15 AM|LINK
OK I'll try to explain. the reason you have to move the file is if you create a custom page that is to be used with a particular table then the correct behavoir is to move it to the ~/DynamicData/CustomPage/TableName/Page.aspx this is the default behavoir. DD routes always translate to the ~/DynamicData folder if there is a CustomePages folder for that table it will look there fist for the relavent page if not it will use a page from the PageTemplates folder.
OK so far.
Now if you have add a new route into the equasion it's still going to look at thing in the above way unless you make it clear in your route that the file is outside the ~/DynamicData folder.
See my example of running two models Dynamic Data – Registering Multiple Models and David Ebbo's Using Dynamic Data with multiple databases these may be of some help.
So if I had a page ~/Articles.aspx (in the root) of the site I would not need to provide a route to acces this file unless I wanted to have the paramter slug passed in as part of the url:
~/Articles.aspx?slug=10
So if I wanted to have a url like:
~/Articles.aspx/10
or
~/Articles/10
the I could add my own route handler, because if you look at a route:
routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }), Model = model });As you can see the route is a DynamicDataRoute.
For details non DD route see Mike Ormond's Getting ASP.NET Routing Up and Running - The Definitive Guide
Dynamic Data routing
Always seeking an elegant solution.