获取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(); } }