I would like to suggest an answer.
Here is the code
public class ValidateSubmitOnceTokenAttribute : ActionFilterAttribute
{
public String ErrorView { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
String submitOnceToken = null;
submitOnceToken = filterContext.HttpContext.Request[ViewHelper.SubmitOnceIdentifier];
if ((bool)filterContext.HttpContext.Session[ViewHelper.SubmitOnceIdentifier + submitOnceToken])
{
if (String.IsNullOrEmpty(View))
{
filterContext.Result = new EmptyResult();
}
else
{
ViewResult newView = new ViewResult();
newView.ViewName = ErrorView;
filterContext.Result = newView;
}
}
else
{
filterContext.HttpContext.Session[ViewHelper.SubmitOnceIdentifier + submitOnceToken] = true;
}
}
}
public partial class ViewHelper
{
internal const string SubmitOnceIdentifier = "_SUBMIT_ONCE_";
public static MvcHtmlString SubmitOnceToken()
{
Guid submitOnceToken = Guid.NewGuid();
HttpContext.Current.Session[SubmitOnceIdentifier + submitOnceToken] = false;
return new MvcHtmlString("<input type="hidden" name="" + SubmitOnceIdentifier + "" value="" + submitOnceToken.ToString() + "">");
}
}
之后,你刚刚需要将这一属性纳入你的方法。
[ValidateSubmitOnceToken(View="ErrorSubmitOnce")]
public ActionResult MyAction(Model) {
....
}
并将此纳入你的看法
@ViewHelper.SubmitOnceOnlyToken()
这一答案使用Aliostad描述的技术,但使用行动过滤器实施。