简体   繁体   中英

Injecting IHostingEnvironment in Razor Views in ASP.NET Core

I am reading website meta tags from XML file which is stored in wwwroot folder. The way to access it is using IHostingEnvironment but since it's on shared _layout how do i inject it. I get error

"The name IHostingEnvironment doesn't exist in current context.

is there a way to set meta tags using XML for _layout so it appears in all Asp.Net pages that share this _layout .

_layout.cshtml

@using Jaeger.Services
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
@inject MenuMasterService menus
@using System.Xml.Linq;

    <!doctype html>
    <html lang="de">

    <head>
        @{

            var filepath = IHostingEnvironment.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()
                         };
            //jaeger
            var xml_tilte = result.ElementAt(0).title;
            var xml_desc = result.ElementAt(0).description;

        }

        <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"] - @xml_tilte</title>
        <meta name="description" content="@xml_desc">


.....

As per @Daniel A. White suggestion, i wrote a custom service and was able to read file. Adding custom service here, it might help anyone else.

MetaReaderService.cs

namespace Test.Services
{
    public class MetaReaderService
    {
        private readonly IHostingEnvironment _env;

        public MetaReaderService(IHostingEnvironment environment)
        {
            _env = environment;
        }

        public IEnumerable<MetaDataXMLModel> GetMetaData()
        {

            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 MetaDataXMLModel
                         {
                             title = a.Element("title").Value.Trim(),
                             description = a.Element("description").Value.Trim(),
                             keywords = a.Element("keywords").Value.Trim()
                         };
            
            return result;
      }


    }

    public class MetaDataXMLModel
    {
        public string title { get; set; }
        public string description { get; set; }

        public string keywords { get; set; }

    }
}

_Layout.cshtml

@using Test.Services
@using Microsoft.Extensions.Configuration
@inject MetaReaderService metaReaderXml
@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">

    @{
        @Html.Raw(ViewData["MetaData"]);

        // loading meta title, description and keyword
        var result = metaReaderXml.GetMetaData();
    }

    <title>@ViewData["Title"] - @result.ElementAt(0).title</title>
    <meta name="description" content=" @result.ElementAt(0).description ">
    <meta name="keywords" content=" @result.ElementAt(0).keywords ">

...

more detail about custom services here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM