//常用的上下文对象
            string a = Request.QueryString[""];//获取Get请求发送的变量
            string b = Request.Form[""];//获取Post请求发送的变量
            string c = Request.Cookies[""].ToString();//获取浏览器发送过来的cookie
            string d = Request.HttpMethod;//获取该请求的HTTP方法(get or post)
            string e = Request.Headers.ToString();//获取整个HTTP的报头
            string f = Request.Url.ToString();//获取请求的URL
            string g = Request.UserHostAddress;//用户的IP地址
            string h = RouteData.Values[""].ToString();//获取当前路由的参数 controller  action 
            Cache i = HttpContext.Cache;//获取应用程序缓存库
            HttpSessionStateBase j = HttpContext.Session;//获取访问者的会话库
            HttpContext.Response.Write("");//产生输出
       Request.IsAjaxRequest();判断是否是ajax请求。

 

同一种请求方式下,控制器的action是不能重载的。(如果想重载,就必须使用不同的请求方式)

 

控制器相关
            //【重载1】默认返回与Action同名的视图
            return View();
            //【重载2】返回本控制器下的Add视图
            return View("Add");
            //【重载3】一般传递的参数对象的类型与视图的模型类一致
            return View("Add", vichin);

            return Redirect("/Basic/Index");
            return RedirectToAction("actionName", "ControllerName");
            return RedirectToRoute(new { controller = "Example", action = "Index", ID = "MyID" });
            return PartialView();
            return Content("响应的内容");
            return File(new byte[1], "application/ms-excel",'excelName');

            FileStream fs = new FileStream(Server.MapPath("~/Content/xxx.pdf"),FileMode.Open);
            return File(fs,"application/pdf",'pdfName');

            return JavaScript("<script>alert(“操作成功')</script>");

            return HttpNotFound
            
            //如果请求方式为get,则必须设置JsonRequestBehavior.AllowGet。(ajax的dataType为JSON时,是不支持GET请求的)
            return Json(vichin, JsonRequestBehavior.AllowGet);
            $.ajax({
            type:"get",dataType:"json",url:"/Default/Action1"
            });

            Redirect和return View的区别
            1、Redirect是让浏览器重定向到新的地址;return view是让服务器把指定的cshtml的内容运行渲染后给到浏览器。
            2、Redirect是浏览器和服务器之间发生了两次交互;return View浏览器和服务器之间发生了一次交互。
            3、Redirect由于是两次请求,所以第一次设置的ViewBag等信息在第一次是取不到的;而View则是在同一个请求中,所以ViewBag信息是可以取到的。
            4、如果用Redirect,则由于是新的对Controller/Action的请求,所以对应的Action会被执行到。如果用View,则是直接拿某个View去显示,对应的Action是不执行的。
            什么情况用View?服务器端产生数据,想让一个View去显示的时候。
            什么情况用Redirect?让浏览器去访问另外一个页面的时候。
常用的返回值

相关文章:

  • 2021-04-09
  • 2021-11-14
  • 2022-12-23
  • 2021-11-16
  • 2021-07-02
  • 2021-11-03
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2021-12-01
  • 2021-12-17
  • 2021-08-03
  • 2021-12-15
  • 2021-09-02
  • 2022-12-23
相关资源
相似解决方案