• asp.net web core 开发问题汇总(1)


     ASP.NET Core 设置默认静态起始页

       转载地址:ASP.NET Core 设置默认起始页(如default.html)

        注:1  默认情况下ASP.NET Core应用程序时不支持静态文件的。

               2   为静态文件提供存储的默认路径时wwwroot;【一定要新建wwwroot的文件夹】

    2   转载自 .net core实现读取appsettings.json配置文件    

    3     转载自 .net core webapi 只允许POST

       A   画线内容没有达到效果

    通过 HttpMethodRouteConstraint 路由约束可以轻松搞定,以下是 asp.net core 3.0 的示例代码

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}",
            constraints: new RouteValueDictionary(new { httpMethod = new HttpMethodRouteConstraint("POST") }));
    });
    B Cotroller 文件中 不实现HttpGet方法 (访问会显示404错误)
    4 转载 [.NET] 利用 async & await 进行异步 操作
    5 读取post参数时,报错以下错误
    Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.
    翻译:不允许同步操作。调用ReadAsync或将AllowSynchronousIO设置为true。
    原因:.net core 默认是异步读取Stream,不允许同步。
    解决:
    // 适用于.net core 3.0
    string
    s = ""; using (var buffer = new MemoryStream()) { Request.EnableBuffering(); Request.Body.Position = 0; // Request.Body.CopyTo(buffer); 修改前
    Request.Body.CopyToAsync(buffer); //修改后
     byte[] b = buffer.ToArray();
    s = System.Text.Encoding.UTF8.GetString(b, 0, b.Length);
    }

     6  设置appsetttings.json文件目录为网站根目录。

         直接 ‘网站根路径’+appsetttings.json

                 提示错误 The configuration file 'appsettings.json' was not found and is not optional

        .net Core 3.0 中不支持  目录+名称 的方式。        

      Configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory()) //添加这行
                    .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
                    .Build();

              Directory.GetCurrentDirectory() 当前工作的目录

    7  json字符串放入key/Value列表   

     var postparamlist = new Dictionary<string, string>();
     var jsonDocument = JsonDocument.Parse(postparamjson);
     for (int i = 0; i < jsonDocument.RootElement.GetArrayLength(); i++)
           {
              var curElement = jsonDocument.RootElement[i];
              var key = curElement.GetProperty("name").ToString();
              var value = curElement.GetProperty("value").ToString();    
               postparamlist.Add(key, value);
           }

     8 json文件读写

    转载自https://blog.csdn.net/a15236307900/article/details/72130032

    关键代码:

    string all = jo.ToString();

    string neame= jo["name"].ToString();

    int age = int.Parse(jo["age"].ToString());

    string city = jo["address"]["city"].ToString();

    string baiduUrl = jo["links"][1]["url"].ToString();

    所赋值可以是string,int,boolean,JTken,JObject.,JArray

    创建一个空("{ }")的JObject对象,通过一定的顺序和方法,将原jo中的数据赋值到空JObject,可以实现增删排序等效果.

    9  json写入 提示“”Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.“”

     转载自https://blog.csdn.net/zhouyingge1104/article/details/83307637

     关键点 

    如果是对象

    //{"code":200,"检测编号":"JC1810231520411","message":"OK"

    string resp =""

    JObject respObj = (JObject)JsonConvert.DeserializeObject(resp);

    如果是字符串

    var str = "[{"" + key + "":"" + value + ""}]";
    var a=JArray.Parse(str);

     10  System.Text.Json 中文乱码问题

    Newtonsoft.Json 一直使用的就是非严格模式咯, 而我们习惯使用的也是这种模式.

    string bJsonString = System.Text.Json.JsonSerializer.Serialize(
    value: jsonObject, options: new System.Text.Json.JsonSerializerOptions {
    //Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Create(allowedRanges: UnicodeRanges.All) 第一种 Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
    第二种
    });
  • 相关阅读:
    20145335 《信息安全系统设计基础》第十四周学习总结
    20145335 《信息安全系统设计基础》第十三周学习总结
    使用MarkdonPad2学习心得
    《信息安全系统设计基础》实验三实验报告
    很久没发博客了
    20145334《信息安全系统设计基础》课程总结
    20145334 《信息安全系统设计基础》第十四周学习总结
    20145334《信息安全系统设计基础》第十三周学习总结
    信息安全系统设计基础实验五实验报告
    《信息安全系统设计基础》实验一 开发环境的熟悉
  • 原文地址:https://www.cnblogs.com/forearm/p/11749646.html
Copyright © 2020-2023  润新知