Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将元数据从XML文件读入共享布局-ASP.NET核心_C#_Asp.net Mvc_Asp.net Core_Razor Pages - Fatal编程技术网

C# 将元数据从XML文件读入共享布局-ASP.NET核心

C# 将元数据从XML文件读入共享布局-ASP.NET核心,c#,asp.net-mvc,asp.net-core,razor-pages,C#,Asp.net Mvc,Asp.net Core,Razor Pages,我正在使用ASP.NET核心razor页面开发一个应用程序。我已将元数据保存在XML文件中;我将其保存为xml的原因是,在CMS系统中,我为用户提供了写入和读取xml文件的选项,以便用户能够更改元数据 现在我想在shared _layout.cshtml中读取并显示元数据标题和描述,以便在所有页面上显示相同的描述。由于_layout.cshtml没有相关的.cs文件来编写C代码,因此我无法读取XML文件 我可以在XDocument共享的index.cshtml.cs等单独页面中阅读,但不能在所有

我正在使用ASP.NET核心razor页面开发一个应用程序。我已将元数据保存在XML文件中;我将其保存为xml的原因是,在CMS系统中,我为用户提供了写入和读取xml文件的选项,以便用户能够更改元数据

现在我想在shared _layout.cshtml中读取并显示元数据标题和描述,以便在所有页面上显示相同的描述。由于_layout.cshtml没有相关的.cs文件来编写C代码,因此我无法读取XML文件

我可以在XDocument共享的index.cshtml.cs等单独页面中阅读,但不能在所有页面的shared\u layout.cshtml中阅读

index.cshtml.cs

_layout.cshtml

不确定您的实体是什么,有关如何在shared\u Layout.cshtml中显示元数据标题和描述,您可以参考以下代码:

@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment _env;
@using System.Xml.Linq;

@{
    var filepath = _env.ContentRootPath.ToString() + @"\meta-data.xml";

    XDocument xmlDoc = XDocument.Load(filepath);

    IEnumerable<XElement> metaList = xmlDoc.Root.Elements("meta");

    var result = from a in metaList
                 select new
                 {
                     title = a.Element("title").Value.Trim(),
                     description = a.Element("description").Value.Trim(),
                     keywords = a.Element("keywords").Value.Trim()
                 };

    ViewData["xml_title"] = result.ElementAt(0).title;
    ViewData["xml_description"] = result.ElementAt(0).description;
    ViewData["xml_keywords"] = result.ElementAt(0).keywords;     
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    @*<meta name="format-detection" content="telephone=no">*@
    <title>@ViewData["Title"] - @ViewData["xml_title"]</title>
    <meta name="description" content="@ViewData["xml_description"] ">
    <meta name="keywords" content="@ViewData["xml_keywords"] ">
.....

这不是一个好主意,代码将在每次页面加载时执行。将数据加载一次,或者至少缓存一次,然后将其注入pageThank@Rena,这将更有意义。我会试试这个。我也同意你的看法。我会把它变成一种服务。如果我注入临时服务,它会加载一次吗?嗨@AmirDora,如果我的答案对你有效,你能接受作为答案吗?如果不能,请跟进让我知道。参考:。
@using Jaeger.Services
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@inject MenuMasterService menus
<!doctype html>
<html lang="de">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    @*<meta name="format-detection" content="telephone=no">*@
    <title>@ViewData["Title"] - @ViewData["xml_title"]</title>
    <meta name="description" content="@ViewData["xml_description"] ">
    <meta name="keywords" content="@ViewData["xml_keywords"] ">
....
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment _env;
@using System.Xml.Linq;

@{
    var filepath = _env.ContentRootPath.ToString() + @"\meta-data.xml";

    XDocument xmlDoc = XDocument.Load(filepath);

    IEnumerable<XElement> metaList = xmlDoc.Root.Elements("meta");

    var result = from a in metaList
                 select new
                 {
                     title = a.Element("title").Value.Trim(),
                     description = a.Element("description").Value.Trim(),
                     keywords = a.Element("keywords").Value.Trim()
                 };

    ViewData["xml_title"] = result.ElementAt(0).title;
    ViewData["xml_description"] = result.ElementAt(0).description;
    ViewData["xml_keywords"] = result.ElementAt(0).keywords;     
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    @*<meta name="format-detection" content="telephone=no">*@
    <title>@ViewData["Title"] - @ViewData["xml_title"]</title>
    <meta name="description" content="@ViewData["xml_description"] ">
    <meta name="keywords" content="@ViewData["xml_keywords"] ">
.....