Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式将HTTP请求从GET重定向到POST_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 以编程方式将HTTP请求从GET重定向到POST

C# 以编程方式将HTTP请求从GET重定向到POST,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,下面是POST控制器操作的方法签名 [System.Web.Mvc.HttpPost] public ActionResult Find(AgentViewModel agent) POST方法接受视图模型对象作为输入参数。然而,GET方法自然是空的,如下所示: public ActionResult Find() 我们希望能够从两个页面访问POST操作。从一页开始,我们使用一个表单。表单数据被序列化到视图模型对象中,并相应地调用操作。但是,第二个页面包含一个带有id

下面是
POST
控制器操作的方法签名

    [System.Web.Mvc.HttpPost]
    public ActionResult Find(AgentViewModel agent)
POST
方法接受视图模型对象作为输入参数。然而,
GET
方法自然是空的,如下所示:

    public ActionResult Find()
我们希望能够从两个页面访问
POST
操作。从一页开始,我们使用一个表单。表单数据被序列化到视图模型对象中,并相应地调用操作。但是,第二个页面包含一个带有
id
值的超链接,该值作为查询字符串追加(
id
是视图模型对象的属性)

因此,本质上我们试图通过两种不同的方式访问
Find
POST
方法:通过将数据序列化到视图模型对象中的表单,以及通过包含查询字符串的超链接

我的问题有两个:我在controllers
Initialize
方法上有一个覆盖,该方法检查
RequestContext
对象是否存在查询字符串。由于对象尚未序列化,如果找到参数,我希望创建视图模型对象并将其序列化到
HTTPRequest
中。由于请求最初被发送到
GET
方法,因此我还想重定向到
POST
操作

对于这个问题,有更简单的破解/解决方法(即添加空参数,在
GET
中使用重定向),但是我想找到一个简单的解决方案来定制管道,作为一个挑战。非常感谢您的指导/意见

    protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);
        if (requestContext.RouteData.Values["action"].ToString() == "Find" &&
            requestContext.RouteData.Values.Count > 2)
        {
            if (requestContext.RouteData.Values["id"] != null)
            {
                string agentId = requestContext.RouteData.Values["id"].ToString();
                AgentViewModel model = new AgentViewModel();
                model.AgentId = agentId;

                //Need to find a way to redirect from the GET to the POST method
                //Need to find a way to add the AgentViewModel object to the request

                ActionInvoker.InvokeAction(this.ControllerContext, "Find");
            }
        }
    }

如果您的html中没有超链接,而是有一个带有提交按钮的表单,这不是更容易吗?这将直接进入post请求,id仍然可以出现在查询字符串中。正如我在帖子中所说的,我想找到一个解决方案,定制一些正在进行的挑战/学习体验。