Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a URL with URL Routing in Webforms

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

like image 787
Armstrongest Avatar asked Nov 11 '08 01:11

Armstrongest


2 Answers

Thanks for the answers. TO add to this, here is what I've done:

In Global.asax

RouteValueDictionary rvdSiteDefaults 
    = new RouteValueDictionary { { "country", "ca" }, { "lang", "en" } };

Route oneArticle 
    = new Route("{country}/{lang}/articles/a{id}/{title}",
        rvdSiteDefaults,
        rvdConstrainID,
        new ArticleRouteHandler("~/Articles/Details.aspx"));

Routes.Add( "Article", oneArticle);       

Create Url from Article object

public static string CreateUrl(Article a) {
    // Note, Article comes from Database, has properties of ArticleID, Title, etc.
    RouteValueDictionary parameters;

    string routeName = "Article"; // Set in Global.asax

    parameters 
      = new RouteValueDictionary { 
         { "id", a.ArticleID }, 
         { "title", a.Title.CleanUrl() } 
        }; 

CleanUrl() returns a URL Friendly name.

    VirtualPathData vpd = RouteTable.Routes.GetVirtualPath(null, routeName, parameters);

    string url = vpd.VirtualPath; 
    return url; // eg. /ca/en/1/The-Article-Title
}

TaDa!

like image 178
Armstrongest Avatar answered Oct 18 '22 02:10

Armstrongest


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:

Dim routedurl = RouteTable.Routes.GetVirtualPath(context, rvd).VirtualPath

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:

requestContext.HttpContext.Items("RequestContext") = requestContext

and then restore it again in your IHttpHandler (aspx page) when required:

Dim rvd = 
  New RouteValueDictionary(New With {.country = "UK", .lang = "EN-GB"})
Dim routedurl = 
  RouteTable.Routes.GetVirtualPath(context.Items("RequestContext"), rvd).VirtualPath

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.

like image 37
MikeO Avatar answered Oct 18 '22 01:10

MikeO