tring key1=Request.Query["key1"];//获取url字符串 String key2 = Request.Form["key2"];//获取表单 String key3 = Request.Cookies["key3"];//获取cookie String key4 = Request.Headers["key4"];//获取http头参数
注:在.net core 2.1中使用cookie需要设置options.CheckConsentNeeded = context => false;即:
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false; //设置false可以打开cookie
options.MinimumSameSitePolicy = SameSiteMode.None;
});
在.net core中使用session,需要在Startup.cs文件进行中间件配置等操作,红色为需要添加的内容:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<MyContext>(options =>
{
options.EnableSensitiveDataLogging(true);//敏感数据记录,可以控制台生成的sql实际传的参数显示出来
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), opts =>
{
//默认情况下SqlServer限制在1000条,假设1500条批量操作会按2个批次来插入数据库,第一批次1000条,第二批次500条来进行处理
//opts.MaxBatchSize(100000);//设置批量操作一批次大小,比如AddRange批量添加一批次插入数据大小限制
//opts.CommandTimeout(72000);//设置超时20分钟
});
});
services.AddSession();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
//设置session
HttpContext.Session.SetString("sessionid", "123456789");
//设置cookie
Response.Cookies.Append("key3", "heheda");
|
1
2
3
4
5
|
//获取sessionString sessionid = HttpContext.Session.GetString("sessionid");//HttpContext.Session.Remove("sessionid");//删除session//获取cookieString key3 = Request.Cookies["key3"]; |
数据绑定:
|
1
2
3
4
5
6
7
8
9
|
/// <summary> /// 数据绑定 /// </summary> public IActionResult SayHello1(String name,int age,string val) { //get请求: http://localhost:5000/Home/SayHello1?name=zhangsan&age=11&val=abc //post请求:使用PostMan测试,Content-Type:application/x-www-form-urlencoded 表单请求可以获取 return Content("hello:"+name+",age:"+age+",val:"+val); } |
模型绑定:查询字符串或表单key的名称和类属性的名称保持一致就能映射上
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//模型绑定:查询字符串或表单key的名称和类属性的名称保持一致就能映射上 public IActionResult SayHello2(TestModel model) { //get请求: http://localhost:5000/Home/SayHello1?name=zhangsan&age=11 //post请求:使用PostMan测试,Content-Type:application/x-www-form-urlencoded 表单请求可以获取 return Content("hello:" + model.Name + ",age:" + model.Age ); } public class TestModel { public String Name { get; set; } public int Age { get; set; } } |

|
1
2
3
4
5
|
//限制了只允许通过url字符串和http正文模型绑定参数public IActionResult SayHello2([FromQuery][FromBody]TestModel model) { return Content("hello:" + model.Name + ",age:" + model.Age ); } |
不是所有的数据都能自动绑定到方法参数上,比如Header就需要制定FromHeaderAttribute.这里测试Header提交的参数并不能进行模型绑定:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>PostHeader</title> </head><body><button onclick="postHeader()" >提交带Header数据</button></body></html><script src="~/lib/jquery/dist/jquery.js"></script><script> function postHeader() { $.ajax({ url: "/Home/PostHeader?r=" + Math.random(), beforeSend: function(xhr) { xhr.setRequestHeader("Name", 'zhangsan'); xhr.setRequestHeader("token", 'djshfdwehh234h3hdjhwhdu23'); }, data: {Name:'呵呵哒',age:12}, type: 'post', success: function(data) { alert(data); } }); }</script> |
后台接收:
|
1
2
3
4
5
6
7
8
|
/// <summary> /// 不是所有的数据都能自动绑定到方法参数上,比如Header就需要制定FromHeaderAttribute.这里测试Header提交的参数并不能进行模型绑定 /// </summary> [HttpPost] public IActionResult PostHeader([FromHeader]String name,[FromHeader]String token,[FromForm]TestModel model) { return Content("hello " + name + ",Name2:"+model.Name+",age2:"+model.Age); } |

|
1
2
3
4
5
6
7
8
|
//返回Json public IActionResult ReturnJson() { //JsonResult result = new JsonResult(new { username = "张三" }); //return result; return Json(new { username = "张三" }); } |

|
1
2
3
4
5
6
7
|
/// <summary> /// 返回指定视图 /// </summary> public IActionResult ShowView() { return View("~/Views/Home/MyTest.cshtml"); } |
