From bc328077b0f780858b51dcfae785535df5063fe4 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Fri, 11 Jan 2019 12:39:48 -0500 Subject: [PATCH] Adding slugify parameter transform (#185) * Adding slugify parameter transform Fixes #172 * Cleaning up code And slugifying my orders --- src/Web/Controllers/ManageController.cs | 2 +- src/Web/Controllers/OrderController.cs | 4 +- src/Web/SlugifyParameterTransformer.cs | 17 +++ src/Web/Startup.cs | 20 +++- .../Manage/{Index.cshtml => MyAccount.cshtml} | 12 +- .../Order/{Index.cshtml => MyOrders.cshtml} | 0 src/Web/Views/Shared/_LoginPartial.cshtml | 103 ++++++++---------- src/Web/Web.csproj | 8 -- 8 files changed, 91 insertions(+), 75 deletions(-) create mode 100644 src/Web/SlugifyParameterTransformer.cs rename src/Web/Views/Manage/{Index.cshtml => MyAccount.cshtml} (77%) rename src/Web/Views/Order/{Index.cshtml => MyOrders.cshtml} (100%) diff --git a/src/Web/Controllers/ManageController.cs b/src/Web/Controllers/ManageController.cs index ed88ef7..07704d7 100644 --- a/src/Web/Controllers/ManageController.cs +++ b/src/Web/Controllers/ManageController.cs @@ -45,7 +45,7 @@ namespace Microsoft.eShopWeb.Web.Controllers public string StatusMessage { get; set; } [HttpGet] - public async Task Index() + public async Task MyAccount() { var user = await _userManager.GetUserAsync(User); if (user == null) diff --git a/src/Web/Controllers/OrderController.cs b/src/Web/Controllers/OrderController.cs index c93aefa..3c314c9 100644 --- a/src/Web/Controllers/OrderController.cs +++ b/src/Web/Controllers/OrderController.cs @@ -20,8 +20,8 @@ namespace Microsoft.eShopWeb.Web.Controllers _orderRepository = orderRepository; } - [HttpGet] - public async Task Index() + [HttpGet()] + public async Task MyOrders() { var orders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(User.Identity.Name)); diff --git a/src/Web/SlugifyParameterTransformer.cs b/src/Web/SlugifyParameterTransformer.cs new file mode 100644 index 0000000..3987c4f --- /dev/null +++ b/src/Web/SlugifyParameterTransformer.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.Routing; +using System.Text.RegularExpressions; + +namespace Microsoft.eShopWeb.Web +{ + + public class SlugifyParameterTransformer : IOutboundParameterTransformer + { + public string TransformOutbound(object value) + { + if (value == null) { return null; } + + // Slugify value + return Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower(); + } + } +} diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index e2d10e7..3c3a437 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.ApplicationCore.Services; @@ -98,7 +99,22 @@ namespace Microsoft.eShopWeb.Web // Add memory cache services services.AddMemoryCache(); - services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + services.AddRouting(options => + { + // Replace the type and the name used to refer to it with your own + // IOutboundParameterTransformer implementation + options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); + }); + + services.AddMvc(options => + { + options.Conventions.Add(new RouteTokenTransformerConvention( + new SlugifyParameterTransformer())); + } + ).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); + + + services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); @@ -163,7 +179,7 @@ namespace Microsoft.eShopWeb.Web { routes.MapRoute( name: "default", - template: "{controller=Home}/{action=Index}/{id?}"); + template: "{controller:slugify=Home}/{action:slugify=Index}/{id?}"); }); } diff --git a/src/Web/Views/Manage/Index.cshtml b/src/Web/Views/Manage/MyAccount.cshtml similarity index 77% rename from src/Web/Views/Manage/Index.cshtml rename to src/Web/Views/Manage/MyAccount.cshtml index fff740a..c8ebf1f 100644 --- a/src/Web/Views/Manage/Index.cshtml +++ b/src/Web/Views/Manage/MyAccount.cshtml @@ -18,15 +18,15 @@ @if (Model.IsEmailConfirmed) { -
- - -
+
+ + +
} else { - - + + } diff --git a/src/Web/Views/Order/Index.cshtml b/src/Web/Views/Order/MyOrders.cshtml similarity index 100% rename from src/Web/Views/Order/Index.cshtml rename to src/Web/Views/Order/MyOrders.cshtml diff --git a/src/Web/Views/Shared/_LoginPartial.cshtml b/src/Web/Views/Shared/_LoginPartial.cshtml index d161a61..5385d36 100644 --- a/src/Web/Views/Shared/_LoginPartial.cshtml +++ b/src/Web/Views/Shared/_LoginPartial.cshtml @@ -1,64 +1,55 @@ -@if (Context.User.Identity.IsAuthenticated) +@if (Context.User.Identity.IsAuthenticated) { -
-
- - -
- - - -
My orders
- -
- - - -
My account
- -
- - - -
Log Out
- -
-
- -
-
- -
- @await Component.InvokeAsync("Basket", User.Identity.Name) -
+
+ @await Component.InvokeAsync("Basket", User.Identity.Name) +
} else { -
-
-
-
+
+
+
+ +
+
+
- - Login - -
-
-
-
- -
- @await Component.InvokeAsync("Basket") -
+
+ @await Component.InvokeAsync("Basket") +
} diff --git a/src/Web/Web.csproj b/src/Web/Web.csproj index 9aebe5c..27899b3 100644 --- a/src/Web/Web.csproj +++ b/src/Web/Web.csproj @@ -76,7 +76,6 @@ <_ContentIncludedByDefault Remove="Views\Manage\EnableAuthenticator.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\ExternalLogins.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\GenerateRecoveryCodes.cshtml" /> - <_ContentIncludedByDefault Remove="Views\Manage\Index.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\ResetAuthenticator.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\SetPassword.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\TwoFactorAuthentication.cshtml" /> @@ -85,7 +84,6 @@ <_ContentIncludedByDefault Remove="Views\Manage\_StatusMessage.cshtml" /> <_ContentIncludedByDefault Remove="Views\Manage\_ViewImports.cshtml" /> <_ContentIncludedByDefault Remove="Views\Order\Detail.cshtml" /> - <_ContentIncludedByDefault Remove="Views\Order\Index.cshtml" /> <_ContentIncludedByDefault Remove="Views\Shared\Components\Basket\Default.cshtml" /> @@ -139,9 +137,6 @@ - - - @@ -154,9 +149,6 @@ - - -