我不熟悉 ASP.NET Core 操作的自定義過濾器屬性。如果使用自定義方法過濾器不存在某些資料,我需要重定向到另一個操作。
這是我的嘗試:
[AttributeUsage(AttributeTargets.Class| AttributeTargets.Method, AllowMultiple = false)]
public class IsCompanyExistAttribute: ActionFilterAttribute
{
private readonly ApplicationDbContext context;
public IsCompanyExistAttribute(ApplicationDbContext context)
{
this.context = context;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//base.OnActionExecuting(filterContext);
if (context.Companies == null)
{
return RedirectToAction(actionName: "Msg", controllerName: "Account",
new { message = "You are not allowed to register, since the company data not exist !!!" });
}
}
我沒用filterContext。RedirectToAction當然,該行顯示為錯誤(帶有紅色下劃線),因為它是 void 方法,而不是操作結果。正如我提到的,我不熟悉自定義過濾器。
請問有什么幫助嗎?
uj5u.com熱心網友回復:
是的。只需將實體的Result屬性設定ActionExecutingContext為您的RedirectToActionResult. 它應該如下所示:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var controller = filterContext.Controller as ControllerBase;
if (context.Companies == null && controller != null)
{
filterContext.Result = controller.RedirectToAction(
actionName: "Msg",
controllerName: "Account",
new { message = "You are not allowed to register, since the company data not exist !!!" }
);
}
base.OnActionExecuting(filterContext);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/409263.html
標籤:
