I know in the MVC Framework, you have the Html Class to create URLs:
Html.ActionLink("About us", "about", "home");
But what if you want to generate Urls in Webforms?
I haven't found a really good resource on the details on generating URLs with Webforms.
For example, if I'm generating routes like so:
Route r = new Route("{country}/{lang}/articles/{id}/{title}",
new ArticleRouteHandler("~/Forms/Article.aspx"));
Route r2 = new Route("{country}/{lang}/articles/",
new ArticleRouteHandler("~/Forms/ArticlesList.aspx"));
Routes.Add(r);
Routes.Add(r2);
How would i generate URLs using the Routing table data.
How do I generate URLS based on my routes?
eg. /ca/en/articles/123/Article-Title without
Thanks for the answers. TO add to this, here is what I've done:
In Global.asax
Create Url from Article object
CleanUrl() returns a URL Friendly name.
TaDa!
As you say, ASP.NET MVC offers you a set of helper methods to "reverse lookup" the RouteTable and generate a URL for you. I've not played with this much yet but as far as I can see you need to call the GetVirtualPath method on a RouteCollection (most likely RouteTable.Routes). So something like:
You need to pass the RequestContext and a RouteValueDictionary. The RouteValueDictionary contains the route parameters (so in your case something like county="UK", lang="EN-GB" etc. The tricky part is the RequestContext as this is not part of the normal HttpContext. You can push it into the HttpContext in your IRouteHandler:
and then restore it again in your IHttpHandler (aspx page) when required:
Apologies for responding to a C# question in VB, it was just that the ASP.NET routing site I had to hand was in VB.NET.
Hyperlink hl = new Hyperlink(); hl.Text = "click here"; hl.NavigateUrl="~/Forms/Article.aspx"; MostlyAnyControl.Controls.Add(hl);
as for putting it in a list... either (1) loop / iterate, or (2) Linq to XML.