Try your search with a different keyword or use * as a wildcard.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Nop.Core;
using Nop.Core.Http.Extensions;
using Nop.Data;
using Nop.Web.Framework.Controllers;
namespace Nop.Web.Framework.Mvc.Filters;
///
/// Represents a filter attribute that saves a selected tabs for tabs
///
public sealed class SaveSelectedTabAttribute : TypeFilterAttribute
{
#region Ctor
///
/// Create instance of the filter attribute
///
/// Whether to ignore the execution of filter actions
/// Whether a message should be persisted for the next request
public SaveSelectedTabAttribute(bool ignore = false, bool persistForTheNextRequest = true) : base(typeof(SaveSelectedTabFilter))
{
PersistForTheNextRequest = persistForTheNextRequest;
IgnoreFilter = ignore;
Arguments = [ignore, persistForTheNextRequest];
}
#endregion
#region Properties
///
/// Gets a value indicating whether to ignore the execution of filter actions
///
public bool IgnoreFilter { get; }
///
/// Gets a value indicating whether a message should be persisted for the next request
///
public bool PersistForTheNextRequest { get; }
#endregion
#region Nested filter
///
/// Represents a filter confirming that checks whether current connection is secured and properly redirect if necessary
///
private class SaveSelectedTabFilter : IAsyncActionFilter
{
#region Fields
protected readonly bool _ignoreFilter;
private bool _persistForTheNextRequest;
protected readonly IWebHelper _webHelper;
#endregion
#region Ctor
public SaveSelectedTabFilter(bool ignoreFilter, bool persistForTheNextRequest,
IWebHelper webHelper)
{
_ignoreFilter = ignoreFilter;
_persistForTheNextRequest = persistForTheNextRequest;
_webHelper = webHelper;
}
#endregion
#region Utilities
///
/// Called asynchronously before the action, after model binding is complete.
///
/// A context for action filters
/// A task that represents the asynchronous operation
private async Task SaveSelectedTabAsync(ActionExecutingContext context)
{
ArgumentNullException.ThrowIfNull(context);
//only in POST requests
if (!context.HttpContext.Request.IsPostRequest())
return;
//ignore AJAX requests
if (_webHelper.IsAjaxRequest(context.HttpContext.Request))
return;
if (!DataSettingsManager.IsDatabaseInstalled())
return;
//check whether this filter has been overridden for the Action
var actionFilter = context.ActionDescriptor.FilterDescriptors
.Where(filterDescriptor => filterDescriptor.Scope == FilterScope.Action)
.Select(filterDescriptor => filterDescriptor.Filter)
.OfType()
.FirstOrDefault();
//ignore filter
if (actionFilter?.IgnoreFilter ?? _ignoreFilter)
return;
var persistForTheNextRequest = actionFilter?.PersistForTheNextRequest ?? _persistForTheNextRequest;
if (context.Controller is BaseController controller)
await controller.SaveSelectedTabNameAsync(persistForTheNextRequest: persistForTheNextRequest);
}
#endregion
#region Methods
///
/// Called asynchronously before the action, after model binding is complete.
///
/// A context for action filters
/// A delegate invoked to execute the next action filter or the action itself
/// A task that represents the asynchronous operation
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await next();
await SaveSelectedTabAsync(context);
}
#endregion
}
#endregion
}