控制器相关

 //常用的上下文对象
            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("");//产生输出

在控制器中接收前台穿过来的参数还可以使用的一种方式

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CustomVariable</title>
</head>
<body>
    <div>
        The controller is: @ViewBag.Controller
    </div>
    <div>
        The action is: @ViewBag.Action
    </div>
    <div>
        The variable is @ViewBag.CustomVariable
    </div>


    <input type="button" id="btn" name="name" value="按钮" />
</body>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $("#btn").click(function () {
            $.ajax({
                type: "post",
                url: "../GetData",
                data: { "name": "vichin", "age": "26", "sex": "" },
                success: function (result) {
                    alert(result);
                }
            });
        });
    });
</script>
</html>

前台代码CustomVariable.cshtml
前台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace UrlsAndRoutes.Controllers
{
    public class HomeController : Controller
    {        
        public ActionResult CustomVariable(string id)
        {
            ViewBag.Controller = "Home";
            ViewBag.Action = "CustomVariable";
            //ViewBag.CustomVariable = RouteData.Values["id"];
            ViewBag.CustomVariable = id;
            return View();
        }
        public ActionResult GetData(string name, int age, string sex)
        {
            var a = name;
            var b = age;
            var c = sex;
            return Content("信息已经收到!");
        }
    }
}

后台代码
控制器中的代码

猜你喜欢

转载自www.cnblogs.com/vichin/p/9363030.html