简体   繁体   English

如何使用 Razor 页面从 ASP.net 核心简单 web 应用程序中的配置读取 json 对象数组

[英]How to read array of json objects from configuration in ASP.net Core simple web app using Razor pages

I am new to.Net and using.Net core to create simple web app.我是 .Net 的新手,使用 .Net 核心创建简单的 web 应用程序。 It using Razor pages for views and its static contents for now.它目前使用 Razor 个页面进行浏览,其 static 个内容。

Here is the project structure,这是项目结构,

在此处输入图像描述

Now, I am into progress of make this page's content to dynamic based on some JSON configuration.现在,我正在根据一些 JSON 配置将此页面的内容动态化。 So, I am just adding things to appsetting.json file to read the json and pass it to views like below, as per Microsoft's documentation.因此,我只是将内容添加到appsetting.json文件以读取 json 并将其传递给如下视图,根据 Microsoft 的文档。

following is my appsettings.json, If I change this VideoProperties urls here, it will reflects everywhere throughout the app.以下是我的 appsettings.json,如果我在这里更改此 VideoProperties url,它将反映在整个应用程序的任何地方。

{
  "AllowedHosts": "*",
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
    "MyVars": [
      {
        "name": "abcd",
        "avatar": "abcd",
        "media": "abcd",
        "category": "abcd",
        "subCategory": "abcd",
        "order": 30,
        "slide": 30
      },
      {
        "name": "abcd",
        "avatar": "abcd",
        "media": "abcd",
        "category": "abcd",
        "subCategory": "abcd",
        "order": 30,
        "slide": 30
      },
      {
        "name": "abcd",
        "avatar": "abcd",
        "media": "abcd",
        "category": "abcd",
        "subCategory": "abcd",
        "order": 30,
        "slide": 30
      },
      {
        "name": "abcd",
        "avatar": "abcd",
        "media": "abcd",
        "category": "abcd",
        "subCategory": "abcd",
        "order": 30,
        "slide": 30
      },
      {
        "name": "abcd",
        "avatar": "abcd",
        "media": "abcd",
        "category": "abcd",
        "subCategory": "abcd",
        "order": 30,
        "slide": 30
      }
    ],
    "VideoProperties": {
      "EmployeeSpeaksVideoUrl": "abcd1P7vBCYAuFczLD6pojJu0a3xYdbdsDhLj",
      "AppSupportVideoUrl": "abcd1nfi9DvdNOlE2tj2CLUdweIpkT8vyfBnh",
      "ClientSpeaksVideoUrl": "abcd1wvLgpCUJg6cTatI5TjIa-eNBI88JcXlt",
      "CloudVideoUrl": "abcd1ipwoyeGjhq8ouwgfx_NsyS2Xd3ZFCnnK",
      "CovidVideoUrl": "abcd1wvLgpCUJg6cTatI5TjIa-eNBI88JcXlt",
      "FunAtWorkVideoUrl": "abcd14wLg8pM2xNtnd5ZZewism8snlLe9A4Ab",
      "KidsAtWorkVideoUrl": "abcd1PuJVNuaxhfvuG7RMqEwVrzJ1VwsEvjIw",
      "BirthdayVideoUrl": "abcd1JjYoe9TGhWU04qzydyz6OtL2ZT6LS8rB",
      "PongalVideoUrl": "abcd10IKZpaAKCLeiyJ2sTAzdESzzyro0DSGx",
      "WomensDayVideoUrl": "abcd1NLe_TNIin9rcekGUtVEpCpoy_4Cl_SFh",
      "NavratriVideoUrl": "abcd19NyLjLLtPBvz-0iYvMsxt9RQrFko8uSd",
      "ChristmasVideoUrl": "abcd1Siwptmm5r_0p7Y6MR6Y6YbCe_zcJ12fS",
      "AnnualPicnicVideoUrl": "abcd1Dd3atu0vRgGQqVFDJ3ZjiH3YNjdBg_4B",
      "CSRVideoUrl": "abcd1wRmwtnesLkFruwVy-BRjCf-8omWeMtcm",
      "GlanceVideoUrl": "abcd1wvLgpCUJg6cTatI5TjIa-eNBI88JcXlt",
      "HRVideoUrl": "abcd1OA0MyrJz3Ti1HYUH4b3R2Q2yxJsGngig",
      "IntroVideoUrl": "abcd14wLg8pM2xNtnd5ZZewism8snlLe9A4Ab",
      "ITSupportVideoUrl": "abcd1XITYeIFwFaleKiO1Hf3E4u5NnYavSvuz",
      "ProductVideoUrl": "abcd1EgQtDl8nO4xe9ePycMbK42d7Q6pOd_Vi",
      "SecurityVideoUrl": "abcd1XITYeIFwFaleKiO1Hf3E4u5NnYavSvuz",
      "TestingVideoUrl": "abcd12Mx7mJN6tgr7Z80uinPd89BhTuIaZTc7",
      "USVideoUrl": "abcd1wvLgpCUJg6cTatI5TjIa-eNBI88JcXlt"
    }
  }

Adding this in Startup.cs file在 Startup.cs 文件中添加

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.Configure<VideoProperties>(Configuration.GetSection("VideoProperties"));
        services.AddRazorPages();
    }

In my Index.cshtml, file I am using like在我的 Index.cshtml 中,我正在使用的文件

@page
@using Microsoft.Extensions.Options;
@inject IOptions<aspnet_core_dotnet_core.Models.VideoProperties> videoUrl
@model IndexModel
@{
    ViewData["Title"] = "Mive ";
}


<video width="100%" autoplay muted loop id="my-video" @*style="max-height: 550px;"*@>
    <source src=@videoUrl.Value.IntroVideoUrl type="video/mp4">
    Your browser does not support HTML5 video.
</video>

This works fine..这工作正常..

I am trying to read an array of json objects (MyVars) in appsettings.json in same way, but I am not getting the values.我正在尝试以相同的方式在 appsettings.json 中读取 json 个对象 (MyVars) 的数组,但我没有得到这些值。

My updated configuration in Startup.cs我在 Startup.cs 中更新的配置

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.Configure<VideoProperties>(Configuration.GetSection("VideoProperties"));
        
        services.Configure<List<MyVars>>(options => Configuration.GetSection("MyVars").GetChildren());
        
        services.AddRazorPages();
    }

In cshtml my code,在 cshtml 我的代码中,

@page
@using Microsoft.Extensions.Options;
@inject IOptions<aspnet_core_dotnet_core.Models.VideoProperties> videoUrl
@inject IOptions<List<aspnet_core_dotnet_core.Models.CeiTour>> tours
@model IndexModel
@{
    ViewData["Title"] = "Mive ";
}


<video width="100%" autoplay muted loop id="my-video" @*style="max-height: 550px;"*@>
    <source src=@videoUrl.Value.IntroVideoUrl type="video/mp4">
    Your browser does not support HTML5 video.
</video>
<div>
    @foreach (var item in @tours.Value)
    {
        <div>@item.Avatar</div>
    }
</div>

My Model for this,为此,我的 Model,

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

namespace aspnet_core_dotnet_core.Models
{
    public class MyVars
    {        
        public string Name { get; set; }
        public string Avatar { get; set; }
        public string Media { get; set; }
        public string Category { get; set; }
        public string SubCategory { get; set; }
        public long Order { get; set; }
        public long Slide { get; set; }
    }
}

I am not getting any output for this for loop in.cshtml file.对于这个 for 循环 in.cshtml 文件,我没有得到任何 output。 I am not sure what is going wrong here.我不确定这里出了什么问题。 How to get the array of json objects in Razor pages and also I need to use thois to same page javascript.如何在 Razor 页中获取 json 个对象的数组,我还需要对同一页 javascript 使用 thois。

Here is a working demo:这是一个工作演示:

Change改变

services.Configure<List<MyVars>>(options => Configuration.GetSection("MyVars").GetChildren());

to

services.Configure<List<MyVars>>(Configuration.GetSection("MyVars"));

RazorPage:剃刀页面:

@page
@inject IOptions<List<MyVars>> l
<div>
    @foreach (var item in l.Value)
    {
        <div>@item.Avatar</div>
    }
</div>

result:结果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用剃刀页面在 ASP.Net Core 上将对象从一个页面传递到另一个页面? - How pass objects from one page to another on ASP.Net Core with razor pages? 简单的注入器注入PageModel ASP.NET核心Razor页面 - Simple Injector Inject into PageModel ASP.NET Core Razor Pages Razor 页面 ASP.NET 核心 - OnPost() 使用 datatables.net 搜索栏不返回任何对象 - Razor Pages ASP.NET Core - OnPost() using datatables.net search bar returns no objects 从ASP.NET CORE Web应用程序中的appsettings.json加载Hangfire配置 - Loading Hangfire configuration from appsettings.json in ASP.NET CORE web app 使用 API 和 razor 页面启动 ASP.NET Core Web 项目 - Starting an ASP.NET Core web project with API and razor pages 如何从 ASP.NET 核心 web 应用程序中获取 JSON 信息 - How to get JSON information from ASP.NET core web app using JavaScript 无法在选择列表中保存多个选择 - asp.net core web app razor pages - Can't save multiple selections in select list - asp.net core web app razor pages ASP.NET Core 2.0 Razor Pages Web应用程序单独的复杂BindProperty表单 - ASP.NET Core 2.0 Razor Pages web app separate Complex BindProperty Form asp.net core 2.0 dotvvm vs razor pages - 对于交互式 Web 应用程序,哪一个更强大? - asp.net core 2.0 dotvvm vs razor pages - which one is more powerful for an interactive web app? 如何将AWS ASP.NET Core日志记录添加到ASP.NET Core 2.0 Razor Pages应用程序? - How do you add AWS ASP.NET Core Logging to an ASP.NET Core 2.0 Razor Pages app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM