tnblog
首页
视频
资源
登录

.NET Core 基础

3387人阅读 2021/5/30 19:45 总访问:94481 评论:0 收藏:0 手机
分类: .NET Core
//1.接收值
            //get的获取值方式
            string submit = Request.Query["usernme"];
           //post的穿参方式
           //在开始运行的时候必须有一个默认值不然会报错
            string submit1 = Request.Form["usernme"];
            //2.获取ip地址
            string ipaddress = HttpContext.Connection.RemoteIpAddress.ToString();
            //获取当前请求方式get,post,put等
            string RequestType =  HttpContext.Request.Method;
            //获取请求地址
            string  Url = HttpContext.Request.Path;
            //获取UserAgent
            string UserAgent = HttpContext.Request.Headers["User-Agent"].FirstOrDefault();
从下面的链接可以知道,微软从ASP.NET Core 3.0开始,在默认情况下,禁用了HttpReqeuest.Body和HttpResponse.Body的同步读写方法,需要设置AllowSynchronousIO为true,才能在HttpReqeuest.Body和HttpResponse.Body上使用同步读写方法,否则就会报错。//解决方法
             var syncIOFeature = HttpContext.Features.Get<IHttpBodyControlFeature>();
                        if (syncIOFeature != null)
                        {
                            syncIOFeature.AllowSynchronousIO = true;
                        }//输出
                        Response.Body.Write(System.Text.Encoding.UTF8.GetBytes("666"));//这种方式的输出需要申明返回类型
            //申明返回的类型     
            Response.ContentType = "text/html;charset=UTF-8";
            //如果不申明上面的类型且使用的输出类型方式为WriteAsync就会以源码的方式输出但汉字会乱码
            Response.Body.WriteAsync(System.Text.Encoding.UTF8.GetBytes("666"));
            Response.Body.WriteAsync(System.Text.Encoding.UTF8.GetBytes("新年快乐"));
            Response.Body.WriteAsync(System.Text.Encoding.UTF8.GetBytes("<br/>"));
            Response.Body.WriteAsync(System.Text.Encoding.UTF8.GetBytes("早生贵子"));        
//获取绝对路径(hostingEnvironment 这种是版本低的已经被弃用但还可以用)
            //1.先注入 private readonly IHostingEnvironment _hostingEnvironment; 在该页面的最上方 接口 Controller 下面
            //2.再在注入好后就在接着的方法中自己跟着定义
            //3.获取项目路径: 注入的名称.ContentRootPath   
            //string rootpath = _hostingEnvironment.ContentRootPath;
            string rootpath = _webHostEnvironment.ContentRootPath;
            //4.获取存储静态资源的wwwroot所在的根目录路径: 注入的名称.WebRootPath   
            //string rootpath = _hostingEnvironment.ContentRootPath;
            string WebRootPath = _webHostEnvironment.WebRootPath;


评价
.
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术