English 中文(简体)
Handle multi Form Submission through ActionFilterAttribute mvc3.net
原标题:Handle Multiple Form Submission via ActionFilterAttribute mvc3.net

I am trying to handle multiple form submissions by writing an ActionFilter. I am new to the domain of ActionFilters and dont know where to start any help will be much appreciated. I have looked at this question but couldnt get a starting point

• 如何处理多份提交书服务器——

最佳回答

我将为此使用反forgery。 你们本应该已经产生(如果你愿意易受欧洲常规武装力量的攻击),而且它对于每一种产生的形式都是独特的。

  1. look in form collection for antiforgery token
  2. look into session["LastFormToken"] (or whatever key you like) - if this form (token) already been submitted
  3. if yes, drop the request, if no (form is submitted first time), put it in session (so next time it will be found there and request will be dropped)
问题回答

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描述的技术,但使用行动过滤器实施。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签