samedi 9 mai 2015

Generate formatted friendly URL from RouteData

I'm trying to programmatically map a query string to an SEO friendly route URL I've created within my RouteConfig.cs file but appear to be having difficultly.

My route configuration looks like this:

routes.MapRoute(
    name: "ReviewPost",
    url: "{year}/{month}/{filename}/",
    defaults: new {controller = "ReviewPost", action = "Detail"},
    namespaces: new[] {"ApplicationName.Website.Controllers"},
    constraints: new {year = @"\d{4}", month = @"\d{2}"}
);

And I'm able to retrieve the correct RouteData object like so:

string url = "~/ReviewPost/Detail/?year=2015&month=05&filename=review-title";
RouteData routeData = RouteDataUtils.RouteByUrl(myUrl);

However I'm having difficulty figuring out how to generate the friendly URL that should look like the one specified in my configuration (/2015/05/review-title/).

Is the RouteData object able to create an instance of the formatted URL above, or do I have to format it myself? Or does the MVC framework have a class for correctly formatting the URL string using the RouteData object?

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire