Skip to content

ogaudefroy/RouteLocalization.WebForms

Repository files navigation

RouteLocalization.WebForms

ASP.Net localized routing for WebForms routes

Build status NuGet version

Declare a localized route

Just add a configured LocalizationRouteCollection in your route tables. This component acts as a wrapper containing all localized routes (neutral included) and maps to a PageRouteHandler.

var routes = new LocalizationRouteCollection(
                virtualPath: "~/pages/offer/details.aspx",
                defaults: null,
                constraints: new RouteValueDictionary() { { "id", @"\d+" } }, });
route.AddTranslation(LocalizationRouteCollection.NeutralRoute, "neutral/neutral-{title}_{id}");
route.AddTranslation("en-US", "job/job-{title}_{id}");
route.AddTranslation("fr-FR", "offre-de-emploi/offre-{title}_{id}");
RouteTable.Routes.Add("OfferDetails", route);

Retrieving route culture

Route culture can be easily retrieved as the culture is added in route values dictionary

protected override void InitializeCulture() {
    var routeCulture = this.RouteData.Values["culture"] as string;
    if (routeCulture != null) {
        this.Culture = new CultureInfo(routeCulture);
        this.UICulture = new CultureInfo(routeCulture);
    }
}

Outbound routing

// Classical way
RouteTable.Routes.GetVirtualPath(null, "OfferDetails", new RouteValueDictionary() {{"title", "chef-de-projet"}, {"id", 12}}).VirtualPath

// With explicit culture
RouteTable.Routes.GetVirtualPath(null, "OfferDetails", new RouteValueDictionary() {{"title", "chef-de-projet"}, {"id", 12}, {"id", "fr-FR"}).VirtualPath

// Through RouteUrlExpressionBuilder
<asp:HyperLink runat="server" NavigateUrl="<%$RouteUrl:title=chef-de-projet,id=12,routename=OfferDetails%>" Text="Details" /> 

Inspired by RouteLocalization