Problems referencing static JavaScript files in wwwroot - .Net Core 2.2 Signalr

北城以北 提交于 2019-12-11 18:00:02

问题


I'm having difficulty referencing the relevant js files for signalr from the wwwroot folder.

The paths I need to reference are:

  • wwwroot/lib/@aspnet/signalr/dist/browser/signalr.js
  • wwwroot/js/chat.js

Here is my view:

@page
<div class="container">
    <div class="row">&nbsp;</div>
    <div class="row">
        <div class="col-6">&nbsp;</div>
        <div class="col-6">
            User..........<input type="text" id="userInput" />
            <br />
            Message...<input type="text" id="messageInput" />
            <input type="button" id="sendButton" value="Send Message" />
        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <hr />
        </div>
    </div>
    <div class="row">
        <div class="col-6">&nbsp;</div>
        <div class="col-6">
            <ul id="messagesList"></ul>
        </div>
    </div>
</div>

<script src="~/wwwroot/lib/@@AspNetCore/signalr/dist/browser/signalr.js"></script>
<script src="~/wwwroot/js/chat.js"></script>

I am getting 404's in the browser:

http://localhost:5005/wwwroot/lib/@AspNetCore/signalr/dist/browser/signalr.js 404 (Not Found)

http://localhost:5005/wwwroot/js/chat.js net::ERR_ABORTED 404 (Not Found)

Update

After changing my script paths I am faced with these errors in the browser:

http://localhost:5005/lib/@AspNetCore/signalr/dist/browser/signalr.js net::ERR_ABORTED 404 (Not Found)

Uncaught ReferenceError: signalR is not defined at chat.js:3

chat.js Line 3:

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

This has red squiggly lines under it.


回答1:


I would recommend you change a folder name and replace a url like this

<script src="~/lib/signalr/dist/browser/signalr.js"></script>
<script src="~/js/chat.js"></script>

Check this link too

Note: make sure your code is place at bottom of the page




回答2:


<script src="~/wwwroot/lib/@@AspNetCore/signalr/dist/browser/signalr.js"></script>
<script src="~/wwwroot/js/chat.js"></script>

Replace with this

@section Scripts {
    <script src="~/lib/@@AspNetCore/signalr/dist/browser/signalr.js"></script>
    <script src="~/js/chat.js"></script>
}



回答3:


1st, you should make sure your app (kestrel server) support static file (app.UseStaticFiles() in Configurate method in startup.cs) , and make sure the Microsoft.AspNetCore.Hosting.IHostingEnvironment.WebRoot is to a folder in your disk(normally it point to $"{Dictory.GetCurrentDirectory()}/wwwroot/").

2st, make sure your signalr.js is in WebRoot folder (so it can be serve), and note the wwwroot folder is your root path eg ~/, which mean your your js file is in wwwroot/lib/signalr/signalr.js the correct path for razor's script Tag Helper is <script src="~/lib/signalr/signalr.js"></script>.



来源:https://stackoverflow.com/questions/57987367/problems-referencing-static-javascript-files-in-wwwroot-net-core-2-2-signalr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!