更新时间:2023-10-29 15:10:58
您可以将过滤器上下文的 Result 设置为 RedirectToRouteResult,而不是获取对 HttpContent 的引用并直接在 ActionFilter 中重定向.它更简洁,更适合测试.
Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It's a bit cleaner and better for testing.
像这样:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(something)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {{ "Controller", "YourController" },
{ "Action", "YourAction" } });
}
base.OnActionExecuting(filterContext);
}