Skip to content

Commit

Permalink
Added support for null values with route defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Gaudefroy committed Nov 9, 2016
1 parent 28a64bd commit 6432bf1
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 10 deletions.
16 changes: 16 additions & 0 deletions RouteLocalization.WebForms.SampleApp/Default.aspx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
*<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RouteLocalization.WebForms.SampleApp.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>
13 changes: 13 additions & 0 deletions RouteLocalization.WebForms.SampleApp/Default.aspx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace RouteLocalization.WebForms.SampleApp
{
using System;
using System.Web.UI;

public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Response.RedirectToRoute("OfferList");
}
}
}
26 changes: 26 additions & 0 deletions RouteLocalization.WebForms.SampleApp/Default.aspx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions RouteLocalization.WebForms.SampleApp/Global.asax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ protected void Application_Start(object sender, EventArgs e)
routeCollection.AddTranslation("fr-FR", "offre-de-emploi/offre-{title}_{id}");
routeCollection.AddTranslation("en-US", "job/job-{title}_{id}");
RouteTable.Routes.Add("OfferDetails", routeCollection);

var routeCollection2 = new LocalizationRouteCollection("~/pages/offer/list.aspx");
routeCollection2.AddTranslation("fr-FR", "offre-de-emploi/liste-offre");
routeCollection2.AddTranslation("en-US", "job/list-job");
RouteTable.Routes.Add("OfferList", routeCollection2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Reference Include="System.EnterpriseServices" />
</ItemGroup>
<ItemGroup>
<Content Include="Default.aspx" />
<Content Include="packages.config" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
Expand All @@ -77,9 +78,17 @@
<ItemGroup>
<Content Include="Global.asax" />
<Content Include="pages\offer\details.aspx" />
<Content Include="pages\offer\list.aspx" />
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Default.aspx.cs">
<DependentUpon>Default.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Default.aspx.designer.cs">
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
Expand All @@ -90,6 +99,13 @@
<Compile Include="pages\offer\details.aspx.designer.cs">
<DependentUpon>details.aspx</DependentUpon>
</Compile>
<Compile Include="pages\offer\list.aspx.cs">
<DependentUpon>list.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="pages\offer\list.aspx.designer.cs">
<DependentUpon>list.aspx</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions RouteLocalization.WebForms.SampleApp/pages/offer/list.aspx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="list.aspx.cs" Inherits="RouteLocalization.WebForms.SampleApp.pages.offer.list" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Just an offerlist</h1>
</div>
</form>
</body>
</html>
17 changes: 17 additions & 0 deletions RouteLocalization.WebForms.SampleApp/pages/offer/list.aspx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RouteLocalization.WebForms.SampleApp.pages.offer
{
public partial class list : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public void LocalizationRouteCollection_OutboundRoute_CultureInThreadTest()
tester.WithRouteInfo("OfferDetails", new { title = "project-manager", id = 12 })
.ShouldGenerateUrl("/neutral/neutral-project-manager_12");
}

}

[Test]
Expand Down Expand Up @@ -216,8 +215,23 @@ public void LocalizationRouteCollection_GetVirtualPathWithNullContext_ThrowsArgu
[Test]
public void LocalizationRouteCollection_GetVirtualPathWithNullValues_ThrowsArgumentNullException()
{
var localizedRoute = CreateLocalizedRoute();
Assert.That(() => localizedRoute.GetVirtualPath(new RequestContext(), null), Throws.InstanceOf<ArgumentNullException>());
var localizedRoute = new LocalizationRouteCollection("~/pages/offer/list.aspx");
localizedRoute.AddTranslation("en-US", "job/job-list");
localizedRoute.AddTranslation("fr-FR", "offre-de-emploi/liste-offre");

var routes = new RouteCollection();
routes.Add("OfferList", localizedRoute);

var tester = new RouteTester(routes);

using (new CultureScopedContext("en-US"))
{
tester.WithRouteInfo("OfferList").ShouldGenerateUrl("/job/job-list");
}
using (new CultureScopedContext("fr-FR"))
{
tester.WithRouteInfo("OfferList").ShouldGenerateUrl("/offre-de-emploi/liste-offre");
}
}
}
}
14 changes: 7 additions & 7 deletions RouteLocalization.WebForms/LocalizationRouteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,14 @@ public override VirtualPathData GetVirtualPath(RequestContext requestContext, Ro
{
throw new ArgumentNullException("requestContext");
}
if (values == null)
{
throw new ArgumentNullException("values");
}

string currentCulture = values["culture"] as string ?? Thread.CurrentThread.CurrentUICulture.Name;

string currentCulture = (values?["culture"] as string) ?? Thread.CurrentThread.CurrentUICulture.Name;
var localizationRoute = this.GetLocalizedOrDefaultRoute(currentCulture);
if (localizationRoute == null)
{
return null;
}
var pathData = localizationRoute.GetVirtualPath(requestContext, new RouteValueDictionary(CopyAndRemoveFromValueDictionary(values)));
var pathData = localizationRoute.GetVirtualPath(requestContext, values != null ? new RouteValueDictionary(CopyAndRemoveFromValueDictionary(values)) : null);
return pathData;
}

Expand All @@ -229,6 +225,10 @@ private RouteBase GetLocalizedOrDefaultRoute(string currentCulture)

private static IDictionary<string, object> CopyAndRemoveFromValueDictionary(IDictionary<string, object> values)
{
if (values == null)
{
return null;
}
Dictionary<string, object> routeValueDictionary = new Dictionary<string, object>(values, StringComparer.OrdinalIgnoreCase);
routeValueDictionary.Remove("culture");
return routeValueDictionary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>ASP.Net localized routing for WebForms routes</description>
<releaseNotes>
-0.1.2: Added support for null values with route defaults.
-0.1.1: Support explicit culture addition in outbound routing
-0.1.0: Initial Release
</releaseNotes>
Expand Down

0 comments on commit 6432bf1

Please sign in to comment.