在ASP.NET MVC邮政/重定向/获取模式 [英] Post/Redirect/Get Pattern in ASP.NET MVC

查看:145
本文介绍了在ASP.NET MVC邮政/重定向/获取模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是实现邮政/重定向/ ASP.NET MVC中获取模式的最佳实践?特别是,什么是当你想回重定向到初始动作/控制器做到这一点的最好方法是什么?

What is the best practice for implementing the Post/Redirect/Get pattern in ASP.NET MVC? In particular, what is the best way to do this when you want to redirect back to the initial action/controller?

下面就是我如何目前做这样的:

Here's how I am currently doing this:


  1. 显示表单的用户。

    • 在表单中,使用<%= Html.Hidden(RETURNURL)%>

    • 在行动中,使用计算机[RETURNURL] = Request.Url;

  1. Display form to user.
    • In the form, use <%= Html.Hidden("returnUrl") %>
    • In the action, use ViewData["returnUrl"] = Request.Url;

这GET是完成任务,但感觉这样会导致大量的重复。我也意识到,我大概可以重定向到 Request.UrlReferrer ...

This get's the job done, but it feels like this would result in a lot of duplication. I also realized that I could probably redirect to Request.UrlReferrer...

你猜是解决这个问题的最干净,最理想的方法是什么?

What do you suppose is the cleanest, most ideal method of accomplishing this?

推荐答案

你这样做的方式是好的,但它看起来像你可能会得太多那么一点点。做你的POST动作采取形式帖子来自多个表单?如果不是,为什么一个隐藏的表单字段烦恼呢?你可以用一个简单的 RedirectToAction脱身(MyAction)

The way you're doing this is fine, but it looks like you might be overthinking it a little bit. Do your POST actions take form posts from more than one form? If not, why bother with a hidden form field? You could get away with a simple RedirectToAction("MyAction")

这篇关于在ASP.NET MVC邮政/重定向/获取模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆