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 = ;
// Request.Body.CopyTo(buffer); 修改前
Request.Body.CopyToAsync(buffer); //修改后
 byte[] b = buffer.ToArray();
s = System.Text.Encoding.UTF8.GetString(b, , 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 = ; 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
第二种
});

最新文章

  1. 你应该在开始API开发之前知道的事(上)(翻译)
  2. express 框架之session
  3. Jenkins环境拓扑及部署流程
  4. IE10访问apache 2.4会奇慢的解决办法
  5. Uploadify在MVC中使用方法案例(一个视图多次上传单张图片)
  6. oracel 导入导出
  7. mac(osx) apache无法启动 localhost无法访问服务器[]
  8. [Flux] 3. Actions
  9. char 与 varchar 不同,造成的麻烦
  10. 黑马day11 脏读数据&amp;amp;解
  11. Spring Boot 配置文件 – 在坑中实践
  12. Java+Selenium3框架设计篇5-如何实现邮件发送测试报告
  13. DevExpress v18.1新版亮点——CodeRush for VS篇(二)
  14. AS 阿里巴巴Java开发规约 CheckStyle-IDEA
  15. linux上安装maven
  16. windows党码农在linux下你最需要的软件列表TOP10
  17. Spring MVC配置CORS(解决跨域请求)
  18. 【WebGL】1.WebGL简介
  19. 插耳机对orientation sensor的影响
  20. 线段树——hdu1754I Hate It

热门文章

  1. 前端开发:这10个Chrome扩展你不得不知
  2. C#制作Wincc组件进行配方管理
  3. webapi+Quartz.NET解决若干定时程序同时运行的问题
  4. 全文搜索引擎 Elasticsearch 入门:集群搭建
  5. Linux kernel简介
  6. Linux 使用vim命令编辑文件内容
  7. mybatis实体为什么要提供一个无参的构造函数
  8. ts中基本数据类型(上)
  9. mysql必知必会--MySQL简介
  10. mysql常见问题解决方案