使用Post/Redirect/Get实现Asp.net防止表单重复提交(2)

来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:48 点击:

<h2>Object moved to <a href="/Security?fusername=admin">here</a>.</h2>
</body></html>
在现在大多数的Web应用程序中都使用是Http 302的重定向。Http 1.1说明书中引用HTTP 303就是用来应对这种用户提交表单后可以在浏览器安全的刷新场景。 HTTP 303 意义是这样的:
Used to tell the client that the resource should be fetched using a different URL. This
new URL is in the Location header of the response message. Its main purpose is to
allow responses to POST requests to direct a client to a resource.
 
在Asp.net MVC可以这些去实现一个自定义ActionResult:
/// <summary>
/// SeeOtherRedirectResult
/// </summary>
public class SeeOtherRedirectResult : ActionResult
{
    private string _url;

    /// <summary>
    /// Initializes a new instance of the <see cref="SeeOtherRedirectResult"/> class.
    /// </summary>
    /// <param name="url">Target URL.</param>
    public SeeOtherRedirectResult(string url)
    {
        _url = url;
    }

    /// <summary>
    /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
    /// </summary>
    /// <param name="context">The context in which the result is executed. The context information includes the controller, HTTP content, request context, and route data.</param>
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.StatusCode = 303;
        context.HttpContext.Response.RedirectLocation = _url;
    }
}

然后Action中使用它,来实现Http 303的重定向。:
[HttpPost]
public ActionResult LoginVerify(string fusername, string fpassword)
{
    return new SeeOtherRedirectResult(Url.Action("Index", "Security", new { fusername = fusername }));
}

运行时,我们来看Http Response RAW:
HTTP/1.1 303 See Other
Cache-Control: private
Location: /Security?fusername=admin
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 24 Mar 2012 03:05:37 GMT
Content-Length: 0

完了,希望对您Web开发有帮助。如有任何问题请留言!
 

 作者:Petter Liu
 

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • 浅析.NET下XML数据访问新机制
  • asp.net 面试+笔试题目第1/2页
  • C# 邮件地址是否合法的验证
  • C#高级编程:数据库连接[1]
  • asp.net 设置GridView的选中行的实现代码
  • 经典C++程序1
  • IIS 自动回收导致后台定时器失效的问题解决
  • ASP.NET&#160;GridView列表代码示例
  • 微软ASP.NET站点部署指南(3):使用Web.Config文件的Transforma
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1