-1

assuming i have script.js as:


    $(document).ready(function () {
        do something
    }

and my view is:


    <p> Hello </p>

    @section scripts{   
        @{await (some html help tag Load script.js)}
    }

i want it to be rendered as this output:


        <p> Hello </p>

    <script>
        $(document).ready(function () {
           do something
        }
    </script>

Is there any razor or HTML helper that output the actual java script into the view?

bob mason
  • 677
  • 1
  • 6
  • 11
  • don't use `@scripts`. just drop the `script` tag right where you want it then. Literally use the code in your last snippet. `@scripts` renders the script wherever the `RenderSection` says – Jonesopolis Oct 27 '19 at 02:05
  • I want to keep the JavaScript file separate from the HTML code, but the result should be one HTML file, that's my point of the question. – bob mason Oct 27 '19 at 04:50

1 Answers1

0

While there are several alternatives for separating JavaScript files from views, e.g.:

you could include JavaScript files as requested with the help of File.ReadAllText and IHostingEnvironment, e.g.:

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment env
...
@Html.Raw(File.ReadAllText(env.WebRootPath + "/js/site.js"))
user7217806
  • 2,014
  • 2
  • 10
  • 12