首页 > 解决方案 > 将元数据表单 XML 文件读入共享 _Layout - ASP.NET Core

问题描述

我正在使用 ASP.NET Core 剃须刀页面开发应用程序。我已将元数据保存在 XML 文件中;我将它保存在 xml 中的原因是,在 CMS 系统中,我为用户提供了写入和读取 XML 文件的选项,因此用户将能够更改元数据。

现在我想阅读和显示元数据titledescription共享_layout.cshtml,以便所有页面上都显示相同的描述。由于_layout.cshtml没有关联.cs文件来写入 C# 代码,因此我无法读取 XML 文件。

我可以在单个页面中阅读它,例如index.cshtml.cs我共享的页面,XDocument但不能在_layout.cshtml所有页面中共享。

index.cshtml.cs

public async Task<IActionResult> OnGetAsync()
{
    /*
     * 
     * Reading Meta Title, Description, Keywords
     * 
     */
    var filepath = _env.ContentRootPath.ToString() + @"\meta-data.xml";

    XDocument xmlDoc = XDocument.Load(filepath);

    // Run query
    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()
                         };

    if (entity.DomainId == 1)   // jaeger
    {
         ViewData["xml_title"] = result.ElementAt(0).title;
         ViewData["xml_description"] = result.ElementAt(0).description;
         ViewData["xml_keywords"] = result.ElementAt(0).keywords;
     }
     else
     {
         ViewData["xml_title"] = result.ElementAt(1).title;
         ViewData["xml_description"] = result.ElementAt(1).description;
         ViewData["xml_keywords"] = result.ElementAt(1).keywords;
     }

     .....
}

_layout.cshtml

@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"] ">
....

标签: c#asp.net-mvcasp.net-corerazor-pages

解决方案


不知道你是什么entity,关于如何显示元数据titledescription共享_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"] ">
.....

推荐阅读