当前位置: 首页 > news >正文

找公司做网站注意事项优化系统的软件

找公司做网站注意事项,优化系统的软件,株洲seo主管,网站建设如何设计数据库前言:在与传统的asp.net MVC项目相比,.net core项目在项目目录的文件结构上和功能上与前者都有很大的区别。例如:在.net core中使用Startup.cs取代Global.asax文件用于加载应用程序的配置和各种启动项。appsettings.json取代web.config文件用于存储应用程…

前言:在与传统的asp.net MVC项目相比,.net core项目在项目目录的文件结构上和功能上与前者都有很大的区别。例如:在.net core中使用Startup.cs取代Global.asax文件用于加载应用程序的配置和各种启动项。appsettings.json取代web.config文件用于存储应用程序所需的配置参数等等。。。

OK!步入正题,下面来说一下如何读取Json配置文件中的参数。

第一种:使用IConfiguration接口

我们先在appsettings.json中配置好数据库连接字符串,然后读取它

{

"Connection": {

"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"

},

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft": "Warning",

"Microsoft.Hosting.Lifetime": "Information"

}

},

"AllowedHosts": "*"

}

在控制器中注入IConfiguration接口

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Microsoft.Extensions.Configuration;

namespace Read.json.Controllers

{

[ApiController]

[Route("[controller]")]

public class ReadController : Controller

{

private IConfiguration _configuration;

public ReadController(IConfiguration configuration)

{

_configuration = configuration;

}

[HttpPost]

public async Task ReadJson()

{

//读参

string conn = _configuration["Connection:dbContent"];

return "";

}

}

}

当然也可以读取数组形式的json,一样的先在appsettings.json中写好配置参数,如下:

{

"Connection": {

"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"

},

//------------------------

"Content": [

{

"Trade_name": {

"test1": "小熊饼干",

"test2": "旺仔QQ糖",

"test3": "娃哈哈牛奶"

}

}

],

//------------------------

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft": "Warning",

"Microsoft.Hosting.Lifetime": "Information"

}

},

"AllowedHosts": "*"

}

比如我们想读取test1

string commodity_test1 = _configuration["Content:0:Trade_name:test1"];

第二种:使用IOptions来读取json配置文件

先把NuGet包导进项目:Microsoft.Extensions.Options.ConfigurationExtensions

首先在appsettings.json中添加节点如下

{

"Connection": {

"dbContent": "Data Source=.;Initial Catalog=test;User ID=sa;Password=123456"

},

//------------------------

"Content": [

{

"Trade_name": {

"test1": "小熊饼干",

"test2": "旺仔QQ糖",

"test3": "娃哈哈牛奶"

}

}

],

//------------------------

"Logging": {

"LogLevel": {

"Default": "Information",

"Microsoft": "Warning",

"Microsoft.Hosting.Lifetime": "Information"

}

},

"AllowedHosts": "*",

//==============================

"Information": {

"school": {

"Introduce": {

"Name": "实验小学",

"Class": "中班",

"Number": "15人"

},

"Region": {

"Province": "湖北",

"City": "武汉",

"Area": "洪山区"

},

"Detailed_address": [

{

"Address": "佳园路207号"

}

]

}

}

//==============================

}

然和再建立一个与这个节点"相同"的类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace Read.json

{

public class Information

{

public School school { get; set; }

}

public class School

{

public Introduce Introduce { get; set; }

public Region Region { get; set; }

public List data { get; set; }

}

public class Introduce

{

public string Name { get; set; }

public string Class { get; set; }

public string Number { get; set; }

}

public class Region

{

public string Province { get; set; }

public string City { get; set; }

public string Area { get; set; }

}

public class Detailed_address

{

public string Address { get; set; }

}

}

在Startup中添加如下代码

#region 服务注册,在控制器中通过注入的形式使用

services.AddOptions();

services.Configure(Configuration.GetSection("Information"));

#endregion

控制器中使用:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

using Microsoft.Extensions.Configuration;

using Microsoft.Extensions.Options;

namespace Read.json.Controllers

{

[Route("api/[controller]/[action]")]

[ApiController]

public class ReadController : Controller

{

private IConfiguration _configuration;

readonly Information _Information;

readonly IOptions _options;

public ReadController(IConfiguration configuration,

Information Information,

IOptions options)

{

_configuration = configuration;

_Information = Information;

_options = options;

}

[HttpGet]

public async Task ReadInformation()

{

string Address = _options.Value.school.Region.Province + "-" +

_options.Value.school.Region.City + "-" +

_options.Value.school.Region.Area + "-" +

_options.Value.school.Detailed_address[0].Address + "-" +

_options.Value.school.Introduce.Name + "-" +

_options.Value.school.Introduce.Class + "-" +

_options.Value.school.Introduce.Number;

return Json(Address);

}

[HttpPost]

public async Task ReadJson()

{

string conn = _configuration["Connection:dbContent"];

string commodity = _configuration["Content:0:Trade_name:test1"];

return "";

}

}

}

第三种:这种应该比较常见,任意读取自定义的json文件

首先建立一个json文件

{

"system_version": {

"Edition": ".Net Core 3.0",

"Project_Name": "Read.json"

}

}

再建一个类,封装一个方法

using Microsoft.Extensions.Configuration;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

namespace Read.json

{

public class Json_File

{

public IConfigurationRoot Read_Json_File()

{

//这句代码会读取read_json.json中的内容

return new ConfigurationBuilder().AddJsonFile("read_json.json")

.Build();

}

}

}

在控制器中调用:

[HttpGet]

public async Task ReadSystemVersion()

{

var configuration = _json_File.Read_Json_File();

string system = "使用的是" + configuration["system_version:Edition"] + "的版本" + "," +

"项目名称是" + configuration["system_version:Project_Name"];

return Json(new

{

data = system

});

}

Demo地址:https://github.com/Davenever/Read_Json.git

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

http://www.lbrq.cn/news/2726965.html

相关文章:

  • 安微省建设厅网站seo推广优化外包公司
  • 个人备案能做什么网站电商培训有用吗
  • 网站访问量大打不开舆情视频
  • 用php做动态网站google海外推广
  • 微信做兼职什么网站好6个好用的bt种子搜索引擎
  • 手机wap网站制作免费最全的百度网盘搜索引擎
  • 网站怎么做适配谷歌网站网址
  • 营口网站开发建网站哪个平台好
  • 网站制作样板网站seo检测
  • 网站怎么做隐藏内容seo和sem的联系
  • 郑州最近新闻事件汕头seo快速排名
  • 西安谁家做网站湖南seo
  • 网站建设教育类旧式网站seo网络推广是什么意思
  • 做黄页网站要告我上海百度搜索优化
  • 万江区网站仿做北京seo优化厂家
  • 网站制作预付款会计分录百度下载安装 官方
  • 建设银行确认参加面试网站怎么做营销推广
  • 制作网站的程序如何做宣传推广效果最好
  • 厦门关键词seo排名网站最新国内新闻重大事件
  • wordpress 时间线seo关键词排名报价
  • 知名室内设计网站谷歌排名算法
  • 怎么设计海报图片郑州seo排名工具
  • 网站推广培训哪里好seo整站优化费用
  • 用ae做模板下载网站推广自己的网站
  • 陕西恒立建设集团网站cms自助建站系统
  • 销客多分销小程序价格汕头seo优化培训
  • 营销型网站名词解释网站推广软件免费
  • 国外营销网站厦门网站设计公司
  • 濮阳交友网站开发公司cms系统
  • 佛山找人做网站百度关键词搜索怎么做
  • LPDDR5训练过程
  • [激光原理与应用-283]:理论 - 波动光学 - 电磁波概述
  • PCA降维全解析:从原理到实战
  • docker 安装 使用
  • 稳定且高效:GSPO如何革新大型语言模型的强化学习训练?
  • GPT 解码策略全解析:从 Beam Search 到 Top-p 采样