添加项目文件。
This commit is contained in:
71
TightWiki/Controllers/WikiControllerBase.cs
Normal file
71
TightWiki/Controllers/WikiControllerBase.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using NTDLS.Helpers;
|
||||
using TightWiki.Models;
|
||||
|
||||
namespace TightWiki.Controllers
|
||||
{
|
||||
public class WikiControllerBase(SignInManager<IdentityUser> signInManager, UserManager<IdentityUser> userManager)
|
||||
: Controller
|
||||
{
|
||||
public SessionState SessionState { get; private set; } = new();
|
||||
|
||||
public readonly SignInManager<IdentityUser> SignInManager = signInManager;
|
||||
public readonly UserManager<IdentityUser> UserManager = userManager;
|
||||
|
||||
[NonAction]
|
||||
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
=> ViewData["SessionState"] = SessionState.Hydrate(SignInManager, this);
|
||||
|
||||
[NonAction]
|
||||
public override RedirectResult Redirect(string? url)
|
||||
=> base.Redirect(url.EnsureNotNull());
|
||||
|
||||
[NonAction]
|
||||
protected string? GetQueryValue(string key)
|
||||
=> Request.Query[key];
|
||||
|
||||
[NonAction]
|
||||
protected string GetQueryValue(string key, string defaultValue)
|
||||
=> (string?)Request.Query[key] ?? defaultValue;
|
||||
|
||||
[NonAction]
|
||||
protected int GetQueryValue(string key, int defaultValue)
|
||||
=> int.Parse(GetQueryValue(key, defaultValue.ToString()));
|
||||
|
||||
[NonAction]
|
||||
protected string? GetFormValue(string key)
|
||||
=> Request.Form[key];
|
||||
|
||||
[NonAction]
|
||||
protected string GetFormValue(string key, string defaultValue)
|
||||
=> (string?)Request.Form[key] ?? defaultValue;
|
||||
|
||||
[NonAction]
|
||||
protected int GetFormValue(string key, int defaultValue)
|
||||
=> int.Parse(GetFormValue(key, defaultValue.ToString()));
|
||||
|
||||
/// <summary>
|
||||
/// Displays the successMessage unless the errorMessage is present.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected RedirectResult NotifyOf(string successMessage, string errorMessage, string redirectUrl)
|
||||
=> Redirect($"{GlobalConfiguration.BasePath}/Utility/Notify?SuccessMessage={Uri.EscapeDataString(successMessage)}&ErrorMessage={Uri.EscapeDataString(errorMessage)}&RedirectUrl={Uri.EscapeDataString($"{GlobalConfiguration.BasePath}{redirectUrl}")}&RedirectTimeout=5");
|
||||
|
||||
protected RedirectResult NotifyOfSuccess(string message, string redirectUrl)
|
||||
=> Redirect($"{GlobalConfiguration.BasePath}/Utility/Notify?SuccessMessage={Uri.EscapeDataString(message)}&RedirectUrl={Uri.EscapeDataString($"{GlobalConfiguration.BasePath}{redirectUrl}")}&RedirectTimeout=5");
|
||||
|
||||
protected RedirectResult NotifyOfError(string message, string redirectUrl)
|
||||
=> Redirect($"{GlobalConfiguration.BasePath}/Utility/Notify?ErrorMessage={Uri.EscapeDataString(message)}&RedirectUrl={Uri.EscapeDataString(Uri.EscapeDataString($"{GlobalConfiguration.BasePath}{redirectUrl}"))}");
|
||||
|
||||
protected RedirectResult NotifyOf(string successMessage, string errorMessage)
|
||||
=> Redirect($"{GlobalConfiguration.BasePath}/Utility/Notify?SuccessMessage={Uri.EscapeDataString(successMessage)}&ErrorMessage={Uri.EscapeDataString(errorMessage)}&RedirectTimeout=5");
|
||||
|
||||
protected RedirectResult NotifyOfSuccess(string message)
|
||||
=> Redirect($"{GlobalConfiguration.BasePath}/Utility/Notify?SuccessMessage={Uri.EscapeDataString(message)}");
|
||||
|
||||
protected RedirectResult NotifyOfError(string message)
|
||||
=> Redirect($"{GlobalConfiguration.BasePath}/Utility/Notify?ErrorMessage={Uri.EscapeDataString(message)}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user