获取Http请求参数


客户端表单项:

<form action="Default" method="post" >
    用户名:<input type="text" name="userName" />
    <input type="submit" value="提交" />
</form>
您输入的用户名是:@ViewBag.userName

控制器中接收:

public class TestController : Controller
{
    [HttpGet]
    public ActionResult Default()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Default(string abc)
    {
        ViewBag.userName = Request["userName"];
     或
     ViewBag.userName = HttpContext.Request["userName"];
     或
     ViewBag.userName = System.Web.HttpContext.Current.Request["userName"];
return View(); } }

普通类中接收:

public class RequestContent
{
    public string GetRequestValue(HttpRequestBase request)
    {
        return request["userName"];
    }

    public string GetRequestValue(HttpContextBase context)
    {
        return context.Request["userName"];
    }

    public string GetRequestValue()
    {
        return System.Web.HttpContext.Current.Request["userName"];
    }
}

控制器中可以这样调用他们:

public class TestController : Controller
{
    //
    // GET: /Test/
    [HttpGet]
    public ActionResult Default()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Default(string abc)
    {
        RequestContent request = new RequestContent();

        ViewBag.userName = request.GetRequestValue(Request);
        或
        ViewBag.userName = request.GetRequestValue(HttpContext);
        或
        ViewBag.userName = request.GetRequestValue();
           
        return View(); 
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM