docs: #292 implementing RedirectFromFullUrl

This commit is contained in:
Hamid Reza Mohammadi 2022-03-10 10:25:45 +03:30
parent ddb2cac525
commit 3e144a7743
10 changed files with 147 additions and 2 deletions

View File

@ -111,6 +111,22 @@
<a role="button" onclick="computePoemRhyme(@Request.Query["id"], '#ModifyModel_RhymeLetters')" class="actionlink">محاسبه</a>
</td>
</tr>
<tr>
<td>
<span class="inputlabel">ایندکس نشود</span>
</td>
<td>
<input type="checkbox" asp-for="ModifyModel.NoIndex" />
</td>
</tr>
<tr>
<td>
<span class="inputlabel">از این نشانی (کامل) هم به این صفحه ری‌دایرکت شود</span>
</td>
<td>
<input style="text-align:left;direction:ltr;" asp-for="ModifyModel.RedirectFromFullUrl" />
</td>
</tr>
<tr>
<td>
<span class="inputlabel">توضیح این ویرایش: </span>

View File

@ -131,7 +131,9 @@ namespace GanjooRazor.Areas.Admin.Pages
OldTag = PageInformation.Poem == null ? null : PageInformation.Poem.OldTag,
OldTagPageUrl = PageInformation.Poem == null ? null : PageInformation.Poem.OldTagPageUrl,
RhymeLetters = PageInformation.Poem == null ? null : PageInformation.Poem.RhymeLetters,
Rhythm = PageInformation.Poem == null ? null : PageInformation.Poem.GanjoorMetre == null ? null : PageInformation.Poem.GanjoorMetre.Rhythm
Rhythm = PageInformation.Poem == null ? null : PageInformation.Poem.GanjoorMetre == null ? null : PageInformation.Poem.GanjoorMetre.Rhythm,
NoIndex = PageInformation.NoIndex,
RedirectFromFullUrl = PageInformation.RedirectFromFullUrl,
};
return true;
}

View File

@ -522,6 +522,12 @@ namespace GanjooRazor.Pages
{
if (pageQuery.StatusCode == HttpStatusCode.NotFound)
{
var redirectQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/redirecturl?url={Request.Path}");
if(redirectQuery.IsSuccessStatusCode)
{
var redirectUrl = JsonConvert.DeserializeObject<string>(await redirectQuery.Content.ReadAsStringAsync());
return Redirect(redirectUrl);
}
return NotFound();
}
}

View File

@ -737,6 +737,28 @@ namespace RMuseum.Controllers
return Ok(res.Result);
}
/// <summary>
/// get redirect url for a url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
[HttpGet]
[Route("redirecturl")]
[AllowAnonymous]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> GetRedirectAddressForPageUrl(string url)
{
RServiceResult<string> res =
await _ganjoorService.GetRedirectAddressForPageUrl(url);
if (!string.IsNullOrEmpty(res.ExceptionString))
return BadRequest(res.ExceptionString);
if (string.IsNullOrEmpty(res.Result))
return NotFound();
return Ok(res.Result);
}
/// <summary>
/// get poem by id
/// </summary>

View File

@ -50,6 +50,16 @@
/// </summary>
public string OldTagPageUrl { get; set; }
/// <summary>
/// no index (search engines are blocked to index the page)
/// </summary>
public bool NoIndex { get; set; }
/// <summary>
/// if a page url is changed, store the old URL here to be redirected automatically
/// </summary>
public string RedirectFromFullUrl { get; set; }
/// <summary>
/// a description of the modfication
/// </summary>

View File

@ -39,6 +39,16 @@
/// </summary>
public string HtmlText { get; set; }
/// <summary>
/// no index (search engines are blocked to index the page)
/// </summary>
public bool NoIndex { get; set; }
/// <summary>
/// if a page url is changed, store the old URL here to be redirected automatically
/// </summary>
public string RedirectFromFullUrl { get; set; }
/// <summary>
/// Poet or Cat
/// </summary>

View File

@ -1071,6 +1071,13 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetRedirectAddressForPageUrl(System.String)">
<summary>
get redirect url for a url
</summary>
<param name="url"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.GetPoemById(System.Int32,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean,System.Boolean)">
<summary>
get poem by id
@ -7746,6 +7753,16 @@
old collection page url e.g /saadi/tayyebat
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorModifyPageViewModel.NoIndex">
<summary>
no index (search engines are blocked to index the page)
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorModifyPageViewModel.RedirectFromFullUrl">
<summary>
if a page url is changed, store the old URL here to be redirected automatically
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorModifyPageViewModel.Note">
<summary>
a description of the modfication
@ -7791,6 +7808,16 @@
Html Text
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPageCompleteViewModel.NoIndex">
<summary>
no index (search engines are blocked to index the page)
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPageCompleteViewModel.RedirectFromFullUrl">
<summary>
if a page url is changed, store the old URL here to be redirected automatically
</summary>
</member>
<member name="P:RMuseum.Models.Ganjoor.ViewModels.GanjoorPageCompleteViewModel.PoetOrCat">
<summary>
Poet or Cat
@ -10414,6 +10441,13 @@
<param name="id"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.GetRedirectAddressForPageUrl(System.String)">
<summary>
get redirect url for a url
</summary>
<param name="url"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.DeletePageAsync(System.Int32)">
<summary>
delete page
@ -12948,6 +12982,13 @@
<param name="commentId"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetRedirectAddressForPageUrl(System.String)">
<summary>
get redirect url for a url
</summary>
<param name="url"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPageByUrl(System.String,System.Boolean)">
<summary>
get page by url

View File

@ -75,6 +75,12 @@ namespace RMuseum.Services
/// <returns></returns>
Task<RServiceResult<string>> GetPageUrlById(int id);
/// <summary>
/// get redirect url for a url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
Task<RServiceResult<string>> GetRedirectAddressForPageUrl(string url);
/// <summary>
/// delete page

View File

@ -726,6 +726,7 @@ namespace RMuseum.Services.Implementation
HtmlText = htmlText,
Note = note,
UrlSlug = dbPage.UrlSlug,
NoIndex = dbPage.NoIndex,
}
);

View File

@ -367,6 +367,33 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// get redirect url for a url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public async Task<RServiceResult<string>> GetRedirectAddressForPageUrl(string url)
{
if (url.IndexOf('?') != -1)
{
url = url.Substring(0, url.IndexOf('?'));
}
// /hafez/ => /hafez :
if (url.LastIndexOf('/') == url.Length - 1)
{
url = url.Substring(0, url.Length - 1);
}
url = url.Replace("//", "/"); //duplicated slashes would be merged
var dbPage = await _context.GanjoorPages.Where(p => p.RedirectFromFullUrl == url).AsNoTracking().SingleOrDefaultAsync();
if (dbPage == null)
return new RServiceResult<string>(null); //not found
return new RServiceResult<string>(dbPage.FullUrl);
}
/// <summary>
/// get page by url
/// </summary>
@ -427,7 +454,9 @@ namespace RMuseum.Services.Implementation
UrlSlug = dbPage.UrlSlug,
FullUrl = dbPage.FullUrl,
HtmlText = dbPage.HtmlText,
SecondPoet = secondPoet
SecondPoet = secondPoet,
NoIndex = dbPage.NoIndex,
RedirectFromFullUrl = dbPage.RedirectFromFullUrl,
};
switch (page.GanjoorPageType)
@ -2579,6 +2608,8 @@ namespace RMuseum.Services.Implementation
await _context.SaveChangesAsync();
dbPage.HtmlText = pageData.HtmlText;
dbPage.NoIndex = pageData.NoIndex;
dbPage.RedirectFromFullUrl = string.IsNullOrEmpty(pageData.RedirectFromFullUrl) ? null : pageData.RedirectFromFullUrl;
bool messWithTitles = dbPage.Title != pageData.Title;
bool messWithUrls = dbPage.UrlSlug != pageData.UrlSlug;