ASP表单的动作过滤器

时间:2019-07-17 16:03:33

标签: c# forms asp.net-core razor

我正在尝试为asp形式的方法创建动作过滤器。

到目前为止,我尝试了以下操作,但未执行任何操作:)

if (!string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(actionName) && context.Controller is Web.Areas.Admin.Controllers.ProductController && actionName == "DownloadCatalogAsPdf")
        {
            switch (controllerName)
            {
                case "Product":
                    context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Listing", action = "DownloadCatalogAsPdf" }));
                    break;

            }
        }

我的表格:

<form asp-controller="Product" asp-action="List" method="post">
        <div class="content-header clearfix">
            <h1 class="pull-left">
                @T("Admin.Catalog.Products")
            </h1>
            <div class="pull-right">
                <a asp-action="Create" class="btn bg-blue">
                    <i class="fa fa-plus-square"></i>
                    @T("Admin.Common.AddNew")
                </a>
                <button type="submit" name="download-catalog-pdf" class="btn bg-purple">
                    <i class="fa fa-file-pdf-o"></i>
                    @T("Admin.Catalog.Products.List.DownloadPDF")
                </button>

//and a lot more down here
</form>

控制器方法:

    [HttpPost, ActionName("List")]
    [FormValueRequired("download-catalog-pdf")]
    public virtual IActionResult DownloadCatalogAsPdf(ProductSearchModel model)
    {
        if (!_permissionService.Authorize(StandardPermissionProvider.ManageProducts))
            return AccessDeniedView();

        //a vendor should have access only to his products
        if (_workContext.CurrentVendor != null)
        {
            model.SearchVendorId = _workContext.CurrentVendor.Id;
        }

        var categoryIds = new List<int> { model.SearchCategoryId };
        //include subcategories
        if (model.SearchIncludeSubCategories && model.SearchCategoryId > 0)
            categoryIds.AddRange(_categoryService.GetChildCategoryIds(parentCategoryId: model.SearchCategoryId, showHidden: true));

        //0 - all (according to "ShowHidden" parameter)
        //1 - published only
        //2 - unpublished only
        bool? overridePublished = null;
        if (model.SearchPublishedId == 1)
            overridePublished = true;
        else if (model.SearchPublishedId == 2)
            overridePublished = false;

        var products = _productService.SearchProducts(
            categoryIds: categoryIds,
            manufacturerId: model.SearchManufacturerId,
            storeId: model.SearchStoreId,
            vendorId: model.SearchVendorId,
            warehouseId: model.SearchWarehouseId,
            productType: model.SearchProductTypeId > 0 ? (ProductType?)model.SearchProductTypeId : null,
            keywords: model.SearchProductName,
            showHidden: true,
            overridePublished: overridePublished);

        try
        {
            byte[] bytes;
            using (var stream = new MemoryStream())
            {
                _pdfService.PrintProductsToPdf(stream, products);
                bytes = stream.ToArray();
            }

            return File(bytes, MimeTypes.ApplicationPdf, "pdfcatalog.pdf");
        }
        catch (Exception exc)
        {
            _notificationService.ErrorNotification(exc);
            return RedirectToAction("List");
        }
    }

和我的动作过滤器:

public class UnfreightedActionFilter : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext context)
    {

        //Check Action and controller of the route and use it to redirect to your custom action or do any customization as per your requirement.
        var actionName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName;
        var controllerName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName;

        //Admin Product
        if (!string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(actionName) && context.Controller is Web.Areas.Admin.Controllers.ProductController && actionName == "DownloadCatalogAsPdf")
        {
            switch (controllerName)
            {
                case "Product":
                    context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Listing", action = "DownloadCatalogAsPdf" }));
                    break;

            }
        }

    }

    public void OnActionExecuted(ActionExecutedContext context)
    {
        //No implementation here. Do it as per your requirement
    }

}

因此,我不想使用继承的控制器中的覆盖,因此可以正常工作。我想创建一个动作过滤器来执行此任务。预先感谢。

0 个答案:

没有答案