From 02e09eb82ead8836208efe9e4dced66e831b3b34 Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:41:39 +0300 Subject: [PATCH] blogging: conventional route matching if route prefix not provided --- .../BloggingRouteConstraint.cs | 35 +++++++++++++++++++ .../Volo.Blogging.Web/BloggingUrlOptions.cs | 6 ++++ .../Volo.Blogging.Web/BloggingWebModule.cs | 25 +++++++++++-- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 modules/blogging/src/Volo.Blogging.Web/BloggingRouteConstraint.cs diff --git a/modules/blogging/src/Volo.Blogging.Web/BloggingRouteConstraint.cs b/modules/blogging/src/Volo.Blogging.Web/BloggingRouteConstraint.cs new file mode 100644 index 0000000000..35731a21ad --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Web/BloggingRouteConstraint.cs @@ -0,0 +1,35 @@ +using System; +using System.Linq; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Options; + +namespace Volo.Blogging; + +public class BloggingRouteConstraint : IRouteConstraint +{ + protected BloggingUrlOptions BloggingUrlOptions { get; } + + public BloggingRouteConstraint(IOptions bloggingUrlOptions) + { + BloggingUrlOptions = bloggingUrlOptions.Value; + } + + public virtual bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) + { + if (BloggingUrlOptions.RoutePrefix != "/" || !BloggingUrlOptions.IgnoredPaths.Any()) + { + return true; + } + + var displayUrl = httpContext.Request.GetDisplayUrl(); + + if (BloggingUrlOptions.IgnoredPaths.Any(path => displayUrl.Contains(path, StringComparison.InvariantCultureIgnoreCase))) + { + return false; + } + + return true; + } +} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs b/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs index 619585d359..44414acf41 100644 --- a/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs +++ b/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace Volo.Blogging { @@ -15,6 +16,11 @@ namespace Volo.Blogging set => _routePrefix = value; } + /// + /// Used to specify ignore paths if the route prefix is null or empty. + /// + public List IgnoredPaths { get; } = new (); + private string GetFormattedRoutePrefix() { if (string.IsNullOrWhiteSpace(_routePrefix)) diff --git a/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs b/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs index 3a80ce21d0..1391a5de19 100644 --- a/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs +++ b/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Mvc; @@ -61,6 +62,26 @@ namespace Volo.Blogging .Extensions() .Add(); }); + + Configure(options => + { + options.ConstraintMap.Add("blogNameConstraint", typeof(BloggingRouteConstraint)); + }); + + Configure(options => + { + var bundlingOptions = context.Services.GetRequiredService>().Value; + if (bundlingOptions.Mode != BundlingMode.None) + { + options.IgnoredPaths.Add(bundlingOptions.BundleFolderName); + } + + options.IgnoredPaths.AddRange(new[] + { + "error", "ApplicationConfigurationScript", "ServiceProxyScript", "Languages/Switch", + "ApplicationLocalizationScript" + }); + }); Configure(options => { @@ -70,8 +91,8 @@ namespace Volo.Blogging var routePrefix = urlOptions.RoutePrefix; - options.Conventions.AddPageRoute("/Blogs/Posts/Index", routePrefix + "{blogShortName}"); - options.Conventions.AddPageRoute("/Blogs/Posts/Detail", routePrefix + "{blogShortName}/{postUrl}"); + options.Conventions.AddPageRoute("/Blogs/Posts/Index", routePrefix + "{blogShortName:blogNameConstraint}"); + options.Conventions.AddPageRoute("/Blogs/Posts/Detail", routePrefix + "{blogShortName:blogNameConstraint}/{postUrl}"); options.Conventions.AddPageRoute("/Blogs/Posts/Edit", routePrefix + "{blogShortName}/posts/{postId}/edit"); options.Conventions.AddPageRoute("/Blogs/Posts/New", routePrefix + "{blogShortName}/posts/new"); });