c# - 将元数据表单 XML 文件读入共享 _Layout - ASP.NET Core
问题描述
我正在使用 ASP.NET Core 剃须刀页面开发应用程序。我已将元数据保存在 XML 文件中;我将它保存在 xml 中的原因是,在 CMS 系统中,我为用户提供了写入和读取 XML 文件的选项,因此用户将能够更改元数据。
现在我想阅读和显示元数据title
并description
共享_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"] ">
....
解决方案
不知道你是什么entity
,关于如何显示元数据title
和description
共享_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"] ">
.....
推荐阅读
- python - 无法将数组参数传递给线程
- google-apps-script - 删除 gmail 中的所有 gmail 促销
- arrays - Mule - XML 数组只读取一个元素
- css - 在 SCSS 上使用 webkit
- c++ - 在 Windows Server 2012 上运行 VS2019 编译的应用程序需要什么?
- python - 蟒蛇,硒 | 如何处理范围滑块
- c - 这段代码是如何工作的?我如何得到这个输出?
- c++ - “考特<
cout<<count<<endl;
灵魂会根据条件提供输出,但它没有打印任何东西,导致这种结果的代码中的故障/错误/缺陷是什么?这是我的第一个问题,如果我不能完全理解,对不起。我使用了以下代码,我无法理解这里发生了什么。这是一个简单的输入输出问题。输出为我们提供了有关匹配两个团队
- java - spring 批处理使用两个不同的数据库
- python - FilePath 宏不包含 Pycharm 中的值