From 7e07f114536bc3aa19a1e0eb720f5c71b2f5ccd9 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 20 Jun 2019 08:58:38 +0300 Subject: [PATCH] blog route path optional resolves https://github.com/abpframework/abp/issues/1312 --- .../BloggingTestAppModule.cs | 5 +++ .../Volo.Blogging.Web/BloggingUrlOptions.cs | 31 +++++++++++++++++++ .../Volo.Blogging.Web/BloggingWebModule.cs | 16 +++++++--- .../Pages/Blog/Posts/Edit.cshtml.cs | 3 +- .../Pages/Blog/Posts/detail.js | 9 +++--- 5 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs diff --git a/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs b/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs index 1bbc4a93a3..c8d8cc2642 100644 --- a/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs +++ b/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs @@ -60,6 +60,11 @@ namespace Volo.BloggingTestApp var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.BuildConfiguration(); + Configure(options => + { + options.RoutePrefix = null; + }); + Configure(options => { #if MONGODB diff --git a/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs b/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs new file mode 100644 index 0000000000..0c56d1c566 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Web/BloggingUrlOptions.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Volo.Blogging +{ + public class BloggingUrlOptions + { + private string _routePrefix = "blog"; + + /// + /// Default value: "blog"; + /// + public string RoutePrefix + { + get => GetFormattedRoutePrefix(); + set => _routePrefix = value; + } + + private string GetFormattedRoutePrefix() + { + if (string.IsNullOrWhiteSpace(_routePrefix)) + { + return "/"; + } + + return _routePrefix.EnsureEndsWith('/').EnsureStartsWith('/'); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs b/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs index 550bf059f5..8febd5eec5 100644 --- a/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs +++ b/modules/blogging/src/Volo.Blogging.Web/BloggingWebModule.cs @@ -1,6 +1,7 @@ using Localization.Resources.AbpUi; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; @@ -63,11 +64,16 @@ namespace Volo.Blogging Configure(options => { - //TODO: Make configurable! - options.Conventions.AddPageRoute("/Blog/Posts/Index", "blog/{blogShortName}"); - options.Conventions.AddPageRoute("/Blog/Posts/Detail", "blog/{blogShortName}/{postUrl}"); - options.Conventions.AddPageRoute("/Blog/Posts/Edit", "blog/{blogShortName}/posts/{postId}/edit"); - options.Conventions.AddPageRoute("/Blog/Posts/New", "blog/{blogShortName}/posts/new"); + var urlOptions = context.Services + .GetRequiredServiceLazy>() + .Value.Value; + + var routePrefix = urlOptions.RoutePrefix; + + options.Conventions.AddPageRoute("/Blog/Posts/Index", routePrefix + "{blogShortName}"); + options.Conventions.AddPageRoute("/Blog/Posts/Detail", routePrefix + "{blogShortName}/{postUrl}"); + options.Conventions.AddPageRoute("/Blog/Posts/Edit", routePrefix + "{blogShortName}/posts/{postId}/edit"); + options.Conventions.AddPageRoute("/Blog/Posts/New", routePrefix + "{blogShortName}/posts/new"); }); } } diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs index 2ccf8cff8f..48da1b0c11 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/Edit.cshtml.cs @@ -62,7 +62,8 @@ namespace Volo.Blogging.Pages.Blog.Posts var editedPost = await _postAppService.UpdateAsync(Post.Id, post); var blog = await _blogAppService.GetAsync(editedPost.BlogId); - return Redirect(Url.Content($"~/blog/{WebUtility.UrlEncode(blog.ShortName)}/{WebUtility.UrlEncode(editedPost.Url)}")); + // return Redirect(Url.Content($"~/blog/{WebUtility.UrlEncode(blog.ShortName)}/{WebUtility.UrlEncode(editedPost.Url)}")); + return RedirectToPage("/Blog/Posts/Detail", new { blogShortName = blog.ShortName, postUrl = editedPost.Url }); } } diff --git a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/detail.js b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/detail.js index 1ab4ba1167..bd0ca4938e 100644 --- a/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/detail.js +++ b/modules/blogging/src/Volo.Blogging.Web/Pages/Blog/Posts/detail.js @@ -88,7 +88,7 @@ if (deleteCommentId != '' && deleteCommentId !== undefined) { abp.message.confirm( - l('PostDeletionWarningMessage'), // TODO: localize + l('PostDeletionWarningMessage'), l('AreYouSure'), function (isConfirmed) { if (isConfirmed) { @@ -97,7 +97,9 @@ url: "/Blog/Posts/Delete", data: { id: deleteCommentId }, success: function () { - window.location.replace('/Blog/' + blogShortName); + var url = window.location.pathname; + var postNameBeginning = url.lastIndexOf('/'); + window.location.replace(url.substring(0, postNameBeginning)); } }); } @@ -105,9 +107,6 @@ ); } }); - $('#DeletePostRouteLink').click(function (event) { - console.log("goooo"); - }); $('.updateLink').click(function (event) { event.preventDefault();