How to include .html file into a razor file in Asp.Net core?

Dung Do Tien May 29 2021 318

I have a small project with Asp.Net Core MVC 3.1. 

On the Home page, I have a razor file is Index.cshtml and I also have another file is site.html, this file contains  all style to design layout for Index.cshtml. Now I want to import site.html into Index.cshtml.

I don't want to use .css file to import inside razor view because it makes my site load slowly. So I created site.html to help increase the performance of my site. But I can't import .html file into a razor file.

I tried with @Html.Partial() but it does not work for me!!!!

Index.cshtml

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8" />
    <style>
         @Html.Partial("Site.html")
    </style>
</head>
<body>
      @RenderBody()
</body>
</html>

Site.html

a {
    color: #25354C;
    text-decoration: none;
}

h3 {
    font-size: 16px;
}

.full-width {
    width: 100%;
}

.pull-left {
    float: left !important
}

Please help me. How can I import it???

Have 2 answer(s) found.
  • W

    Weanich Sanchol May 29 2021

    The Html helper tag in Razor view is not supported for load client files such as .html files.

    You have to System.IO.File package to load .html file and use @Html.Raw() to draw content into the razor file. For example:

     @Html.Raw(System.IO.File.ReadAllText("relative_path/site.html"))

    I hope it helpful for you!!

  • M

    Mary Christ May 29 2021

    Do you have to use .html to generate CSS to import a razor file? Generate CSS into .html or .cshtml are the same.  If you change to .cshtml you can import very easily by using Html.PartialAsync() method. Or not you have to read .html by using IO.File and append to razor view.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close