简体   繁体   English

在标准ASP.Net Web应用程序(非MVC)中正确实现SignalR

[英]Proper implementation of SignalR in a standard ASP.Net web application (Non-MVC)

Am currently developing a normal web application using site.master page for the layouts. 目前,我正在使用site.master页面进行布局来开发普通的Web应用程序。

I noticed, when implementing SignalR, we have to import the following scripts 我注意到,在实施SignalR时,我们必须导入以下脚本

<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
<script src="signalr/hubs"></script>

However, is it a good practice to place the scripts in the site.master, considering the connection would start multiple times. 但是,考虑到连接会多次启动,将脚本放置在site.master中是一种好习惯。

Is there a better way to import the scripts in a more general place where it will only start the hub once? 有没有更好的方法可以在更通用的位置导入脚本,该脚本只会启动一次集线器? Or does it not matter at all if I leave it in my site.master page? 还是将它留在site.master页面中根本没有关系吗?

Also, what happens if I have different pages that do not use the site.master, in that case, do I need to re-import the scripts in the different page? 另外,如果我有不同的页面不使用site.master,那该怎么办?在这种情况下,我需要在其他页面中重新导入脚本吗?

There are two ways that comes to mind. 我想到两种方法。

Implementing nested master page. 实现嵌套母版页。 See Here You can add scripts in inner master page and use this master page only for the pages where you need these additional scripts. 请参见此处。您可以在内部母版页中添加脚本,并且仅将此母版页用于需要这些其他脚本的页面。 This is a "ok" approach if there are a lot of pages that needs this script (but not all). 如果有很多页面需要此脚本(但不是全部),则这是一种“好的”方法。

You could also create a content place holder in master template. 您也可以在主模板中创建内容占位符。 like this. 像这样。

<asp:ContentPlaceHolder ID="cphScripts" runat="server">
</asp:ContentPlaceHolder>

Then in the pages where you want to add extra scripts you can add scripts like this. 然后,在要添加其他脚本的页面中,可以添加这样的脚本。

<asp:Content ID="ContentScripts" ContentPlaceHolderID="cphScripts" runat="server">
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/jquery.signalR-2.1.0.min.js"></script>
    <script src="signalr/hubs"></script>
</asp:Content>

I would take this approach if you have few pages where you need these scripts. 如果您需要这些脚本的页面很少,我会采用这种方法。 As you can see you have to repeat the block again and again in every page. 如您所见,您必须在每一页中一次又一次地重复该块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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