【发布时间】:2015-01-09 07:06:39
【问题描述】:
我正在使用 MVC nopCommerce 并开发自定义插件,该插件会覆盖 HomepageBestSellers 的现有功能(ProductController 的操作归因于 [ChildActionOnly])。
FilterProvider:
namespace Nop.Plugin.Product.BestSellers.Filters
{
public class BestSellersFilterProvider : IFilterProvider
{
private readonly IActionFilter _actionFilter;
public BestSellersFilterProvider(IActionFilter actionFilter)
{
_actionFilter = actionFilter;
}
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
if (actionDescriptor.ControllerDescriptor.ControllerType == typeof(ProductController) && actionDescriptor.ActionName.Equals("HomepageBestSellers"))
{
return new Filter[]
{
new Filter(_actionFilter, FilterScope.Action, null)
};
}
return new Filter[] { };
}
}
}
Action Filter:
namespace Nop.Plugin.Product.BestSellers.Filters
{
public class BestSellersFilter : ActionFilterAttribute
{
private readonly ISettingService _settingService;
private readonly IStoreService _storeService;
private readonly IWorkContext _workContext;
public BestSellersFilter(ISettingService settingService,
IStoreService storeService, IWorkContext workContext)
{
this._settingService = settingService;
this._storeService = storeService;
this._workContext = workContext;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//load settings for a chosen store scope and ensure that we have 2 (or more) stores
var storeScope = 0;
if (_storeService.GetAllStores().Count < 2)
storeScope = 0;
var storeId = _workContext.CurrentCustomer.GetAttribute<int>(SystemCustomerAttributeNames.AdminAreaStoreScopeConfiguration);
var store = _storeService.GetStoreById(storeId);
storeScope = store != null ? store.Id : 0;
var bestSellersSettings = _settingService.LoadSetting<BestSellersSettings>(storeScope);
if (bestSellersSettings.IsBestSellersEnabled)
{
filterContext.Result = new RedirectResult("Plugins/BestSellersProducts/PublicInfo");
}
else
base.OnActionExecuting(filterContext);
}
}
}
我在 filterContext.Result = new RedirectResult("Plugins/BestSellersProducts/PublicInfo"); 这一行收到以下错误:
Child actions are not allowed to perform redirect actions.
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Child actions are not allowed to perform redirect actions.
UPDATE:
-
根据答案更改了 BestSellersFilter.cs。
public override void OnActionExecuting(ActionExecutingContext filterContext) { var storeScope = 0; if (_storeService.GetAllStores().Count < 2) storeScope = 0; var storeId = _workContext.CurrentCustomer.GetAttribute<int>(SystemCustomerAttributeNames.AdminAreaStoreScopeConfiguration); var store = _storeService.GetStoreById(storeId); storeScope = store != null ? store.Id : 0; var featuredProductsSettings = _settingService.LoadSetting<FeaturedProductsSettings>(storeScope); if (featuredProductsSettings.IsFeaturedProductsEnabled) { var products = _productService.GetAllProductsDisplayedOnHomePage(); BestSellersController objResult = new BestSellersController(); filterContext.Result = new ContentResult { Content = objResult.PublicInfoPlugin() }; //base.OnActionExecuting(filterContext); } else base.OnActionExecuting(filterContext); } -
根据答案更改了 BestSellersController.cs。
public string PublicInfoPlugin() { var featuredProductsSettings = _settingService.LoadSetting<FeaturedProductsSettings>(_storeContext.CurrentStore.Id); if (featuredProductsSettings.IsFeaturedProductsEnabled) { var products = _productService.GetAllProductsDisplayedOnHomePage(); //ACL and store mapping products = products.Where(p => _aclService.Authorize(p) && _storeMappingService.Authorize(p)).ToList(); //availability dates products = products.Where(p => p.IsAvailable()).ToList(); if (products.Count == 0) return ""; var model = PrepareProductOverviewModels(products.Take(featuredProductsSettings.ShowFeaturedProductsNumber)).ToList(); return RenderPartialViewToString("~/Plugins/Product.FeaturedProducts/Views/ProductFeaturedProducts/PublicInfo.cshtml", model); } return ""; }
现在从 PublicInfoPlugin 方法中的所有私有对象中获取 null 值。
【问题讨论】:
标签: asp.net-mvc runtime-error nopcommerce