简体   繁体   English

从Web Api控制器中的Post方法获取任何参数

[英]Get any parameter from a Post Method in a Web Api controller

I've an old page made in aspx that get every Request.Form.Keys and made a POST using a form to another page (is like an "HTTP POST REDIRECT") 我有一个用aspx制成的旧页面,该页面可以获取每个Request.Form.Keys并使用表单向另一个页面进行POST(就像“ HTTP POST REDIRECT”一样)

Now this system must be migrated to Web API (2.1) and I must emulate the same. 现在,该系统必须迁移到Web API(2.1),并且我必须进行模拟。

The trick with this is that the aspx gets any/multiple keys and values, you can post anything to the aspx. 诀窍在于,aspx获取任何/多个键和值,您可以将任何内容发布到aspx。 But I can't get the same result with my POST method in web api, because it expects the object as is (and I don't know it and I don't care) 但是我在Web api中的POST方法无法获得相同的结果,因为它希望对象保持原样(而且我也不知道,我不在乎)

I don't know if this can help, but the code of the old aspx is like this one: 我不知道这是否可以帮助您,但是旧aspx的代码如下:

        sb.Append("<html>");
        sb.AppendFormat("<body onload='document.forms[0].submit()'>Loading...");
        sb.AppendFormat("<form action='{0}' method='post'>",
            the-next-url);
        foreach (string key in Request.Form.Keys)
        {
            sb.AppendFormat("<input type='hidden' name='{0}' value='{1}'>", key, Request.Form[key]);
        }
        sb.Append("</form>");
        sb.Append("</body>");
        sb.Append("</html>");

        Response.Write(sb.ToString());

Finally, I just use inside my method: 最后,我只在方法内部使用:

HttpContext.Current.Request.Form

to get all POST keys and values instead of Request.Form 获取所有POST键和值而不是Request.Form

You could refactor your code, I suppose, to use something like this: 我想,您可以重构代码以使用如下代码:

        if (HttpContext.Current.Request.Form.Count > 0)
        {
            sb.Append("<html>");
            sb.AppendFormat("<body onload='document.forms[0].submit()'>Loading...");
            sb.AppendFormat("<form action='{0}' method='post'>",your_url);
            foreach (string key in HttpContext.Current.Request.Form.AllKeys)
            {
                sb.AppendFormat("<input type='hidden' name='{0}' value='{1}'>", key,
                    HttpContext.Current.Request.Form[key]);
            }
            sb.Append("</form>");
            sb.Append("</body>");
            sb.Append("</html>");



        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM