检测是否动作是POST或GET方法 [英] Detect if action is a POST or GET method

查看:116
本文介绍了检测是否动作是POST或GET方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 3,是否有可能以确定是否一个动作是一个POST的结果,或者GET方法?我知道你可以装饰用行动[HttpPost]和[HTTPGET]火一个特定的动作,如果其中的一个情况。我想要做的就是离开这些属性关闭,编程方式确定哪一个导致该操作。

In MVC 3, is it possible to determine if an action is a result of a POST or GET method? I know you can decorate the actions with [HttpPost] and [HttpGet] to fire a specific action if one of those happens. What I'd like to do is leave those attributes off and programmatically determine which one caused the action.

究其原因是因为我的搜索页面的架构,我存储TempData的搜索模型的方式。初始搜索导致POST到搜索结果页面,但分页链接都只是链接到/业绩/ 2(第2页)。他们研究TempData的,看看是否模型是有一个使用它,如果如此。

The reason is, because of the way my search page is architected, I'm storing the search model in TempData. The initial search causes a POST to the search results page, but the paging links are all just links to "/results/2" (for page 2). They examine TempData to see if the model is in there an use it if so.

这会导致问题,当有人使用后退按钮转到搜索表单和重新提交。它仍然在TempData的拿起模型,而不是使用新的搜索条件。所以,如果它是一个POST(即有人只是提交搜索表单),我想先清除TempData的。

This causes problems when someone uses the back button to go to the search form and resubmit it. It's still picking up the model in TempData instead of using the new search criteria. So if it's a POST (i.e. someone just submitted the search form), I want to clear out TempData first.

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.httpmethod.aspx\"><$c$c>HttpMethod的Htt prequest 对象属性将得到它。你可以使用:

The HttpMethod property on the HttpRequest object will get it for you. You can just use:

if (HttpContext.Current.Request.HttpMethod == "POST")
{
    // The action is a POST.
}

或者,你可以得到<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.request.aspx\"><$c$c>Request反对直客电流控制器。这只是一个属性。

Or you can get the Request object straight off of the current controller. It's just a property.

这篇关于检测是否动作是POST或GET方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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