我正在扩展ActionResult
课程。在ExecuteResult
方法中,我想检查操作是GET
还是POST
但是ControllerContext
类似乎没有让我这样做的属性。有人知道如何从ControllerContext
检查请求类型吗?
public override void ExecuteResult(ControllerContext context)
{
//See if the request was POST
if (context.HttpContext.Request.?)
{
DoStuff();
}
DoOtherStuff();
}
答案 0 :(得分:2)
你可以使用
context.HttpContext.Request.HttpMethod
http://msdn.microsoft.com/en-us/library/system.web.httprequest.httpmethod%28v=vs.100%29.aspx#Y0
答案 1 :(得分:0)
试试这个:
public HttpVerbs RequestHttpVerb(ControllerContext context)
{
return (HttpVerbs)Enum.Parse(typeof(HttpVerbs), context.HttpContext.Request.HttpMethod, true);
}
public override void ExecuteResult(ControllerContext context)
{
if (this.RequestHttpVerb(context) == HttpVerbs.Post)
{
}
}