静态文件读取和脚本参数

静态文件读取:

  1. Nuget引入:Nuget引入Microsoft.Extensions.FileProviders

  2. 配置读取静态文件的中间件
    在Startup.cs 添加如下代码

//读取静态文件的中间键
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
            });

脚本参数:

  1. 可以在启动dll的脚本之后放上参数,格式:–参数名称=参数值
  2. 控制器通过构造函数注入IConfiguration
  3. 可以在控制器中通过IConfiguration[参数名称]

实例:
在这里插入图片描述
OneController

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication6.Controllers
{
    public class OneController : Controller
    {
        private readonly IConfiguration configuration;
        //通过构造函数注入 IConfiguration
        public OneController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            //读取脚本参数
            ViewBag.a = configuration["port"];
            return View();
        }
    }
}

.cshtml


@{
    ViewData["Title"] = "index";
}
<h1>
    命令参数:@ViewBag.a
</h1>

执行命令:
在这里插入图片描述

运行结果:
在这里插入图片描述

配置文件读取:

第一种:
通过IConfiguration索引取值;
如果遇到对象,数组;
中间使用:分割,依次读取;
在这里插入图片描述

在这里插入图片描述

第二种:

  1. 定义一个和配置文件内容格式一致的类
  2. 在ConfigureServices 配置 services.Configure(Configuration.GetSection(“AppU”));
  3. 在使用的时候可以直接通过IOptions options注入
  4. 获取注入的Options.Value 就是我们获取的到配置文件的一个实体类型对象

新建实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication6.Models
{
    public class Ua
    {
        public string Uname { get; set; }
        public List<string> Uidlist { get; set; }
    }
}

实体类中的属性值对应appsettings.json中的节点
在这里插入图片描述
OneController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication6.Models;

namespace WebApplication6.Controllers
{
    public class OneController : Controller
    {
        private readonly Ua ua;
        private readonly IConfiguration configuration;
        //通过构造函数注入 IConfiguration
        public OneController(IConfiguration configuration,IOptions<Ua> options)
        {
            this.ua = options.Value;
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            //读取脚本参数
            ViewBag.a = configuration["port"];

            //读取appsettings.json 配置文件
            ViewBag.names = configuration["Names"];
            ViewBag.id = configuration["Info:Id"];
            ViewBag.age = configuration["Info:Age"];

            object list = Newtonsoft.Json.JsonConvert.SerializeObject(ua);
            return View(list);
        }
    }
}

one/index.cshtml

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "index";
}
@model object
<h1>
    命令参数:@ViewBag.a
</h1>
<h1>
    姓名:@ViewBag.names
</h1>
<h1>
    编号:@ViewBag.id
</h1>
<h1>
    年龄:@ViewBag.age
</h1>
<h1>
    @Model
</h1>

运行最终结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值