Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to Load Html File in asp.net core Razor Pages

$
0
0

My purpose is  Load  Html  File   in asp.net core Razor Pages .

For example  I have a  Test.html File   in the app folder wwwroot .

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    Hollo!  Asp.net  core   it is Ok !
</body>
</html>

Now I  Add a TestIndex.cshtml  page

@page
@model iaskyy.Pages.TestIndexModel
@{
    ViewData["Title"] = "TestIndex";
}

<h2>TestIndex</h2>
<div id="content">
    <h3>@Html.Raw(@ViewData["fileContent"])</h3> 

</div>

 then    TestIndex.cshtml .cs  

namespace iaskyy.Pages
{

 public class TestIndexModel : PageModel
    {
        private readonly iaskyy.Models.IaskyyDbContext _context;
        private IHostingEnvironment _hostingEnv;

        public TestIndexModel(iaskyy.Models.IaskyyDbContext context, IHostingEnvironment hostingEnv)
        {
            _context = context;
            _hostingEnv = hostingEnv;
        }
        public void OnGet()
        {
            string filen = "test.html";

            var fileName = Path.Combine(_hostingEnv.WebRootPath, filen);

            ViewData["fileName"] = fileName;
            var fileContent = "";
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs))
                    {

                        while (sr.Peek() >= 0)
                        {
                            fileContent = sr.ReadToEnd();

                        }
                    }
                }
            }
            catch { fileContent = "err"; }
            ViewData["fileContent"] = fileContent;
        }
    }

}

Run then core  app in native http://localhost:44371/testindex the result is

TestIndex

Hollo! Asp.net core it is Ok !

The Razor Page load  the test.html  file  success!

Now I Publish the app to  host  IIS  Server.  And run http://www.mywebsite.com/TestIndex  

the result is :

TestIndex

err

The Razor Page load  the test.html  file  err . I dot know  the reaseon why ?Anysuggestion ?

Or  Is  there any other method  can  load  the test.html  file  In The Razor Page ?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>