Asp.net mvc 使用自定义操作筛选器属性时出现意外行为
我试图重现boiler plate account action filter所具有的相同功能,但执行操作所需的值不同。我遇到了一种奇怪的情况。以下是我的Asp.net mvc 使用自定义操作筛选器属性时出现意外行为,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我试图重现boiler plate account action filter所具有的相同功能,但执行操作所需的值不同。我遇到了一种奇怪的情况。以下是我的ActionFilterAttribute定义: public class RequireMemberSelectionAttribute : ActionFilterAttribute { /// <summary> /// When the decorated action is executing, redir
ActionFilterAttribute
定义:
public class RequireMemberSelectionAttribute : ActionFilterAttribute
{
/// <summary>
/// When the decorated action is executing, redirect to the member selection view if a member is not selected.
/// </summary>
/// <param name="a_filterContext">Filter context.</param>
public override void OnActionExecuting(ActionExecutingContext a_filterContext)
{
if ((Session.Current.Preferences.MemberID ?? 0) > 0)
return;
String returnUrl = null;
if (a_filterContext.HttpContext.Request.Url != null)
returnUrl = a_filterContext.HttpContext.Request.Url.PathAndQuery;
a_filterContext.Redirect("Builder", "ListMembers", new { ReturnUrl=returnUrl });
}
}
意外的行为是,当在ListMembers.cshtml
视图上单击链接时,它不会转到SelectMember
操作,而是尝试转到SelectArea
,然后重新开始
以下是ListMembers.cshtml
的确切链接。我从浏览器中的渲染页面复制了它:
我觉得动作过滤器有些地方我不明白
以下是我如何在razor视图中生成链接:
@Html.ActionLink("Test Link", "SelectMember", new { id = 1, returnUrl = ViewBag.ReturnUrl })
如何在
ListMembers.cshtml
中生成链接?这是一个公平的问题。我使用Html.ActionLink
。我在上面的帖子末尾添加了这一行。我会仔细检查一下,如果你这样做,session.Preferences.MemberID=id代码>反映在您的筛选器会话.Current.Preferences.MemberID中的代码>SelectMember操作中的重定向永久性是什么?这是否指示浏览器在请求Builder/SelectMember/1时始终转到Builder/SelectArea?请参阅,我认为RedirectPermanent
意味着当前页面将在历史日志中被覆盖。这是完全不同的。像往常一样,MSDN文档什么也没告诉我。我应该在谷歌上查一下。我该怎么做我想做的事?
@Html.ActionLink("Test Link", "SelectMember", new { id = 1, returnUrl = ViewBag.ReturnUrl })