使用Post/Redirect/Get实现Asp.net防止表单重复提交(2)
<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开发有帮助。如有任何问题请留言!
相关新闻>>
- 发表评论
-
- 最新评论 更多>>