来自控制器初始化的C#MVC访问操作参数

时间:2018-02-27 20:53:23

标签: c# post httpcontext

如果我有一个带有HTTP POST操作的控制器,有一个或几个参数,如下所示:

//[HttpPost]
public ActionResult Ajaxbuscarope(string texto="")
{

}

我怎样才能访问" texto"参数直接来自控制器初始化...

protected override void Initialize(System.Web.Routing.RequestContext 
requestContext)
{

    var texto=???
}

我可以使用这个...

访问Get参数
var url_with_params=System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

但这不适用于声明参数的发布请求

System.Collections.Specialized.NameValueCollection post = System.Web.HttpContext.Current.Request.Form;

1 个答案:

答案 0 :(得分:0)

HttpRequest request = HttpContext.Current.Request;
request.InputStream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(request.InputStream);

var requestFromPost = Encoding.Default.GetString(HttpContext.Current.Request.BinaryRead(HttpContext.Current.Request.TotalBytes));
//this is very important to have the parameters available in the action
request.InputStream.Seek(0, SeekOrigin.Begin);