将数据从ASP.NET MVC控制器推送到视图 [英] Pushing data from an ASP.NET MVC Controller to a View

查看:78
本文介绍了将数据从ASP.NET MVC控制器推送到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将后端构建到一个站点,该站点的前端将有多个小部件",需要实时更新.

I'm building the back end to a site which will have multiple "widgets" on the front end which need to update in real time.

现在,我只是有一个装入方法,该方法显然在页面装入时用数据填充所有小部件.我的问题是如何处理进一步更新的实时方面.

Right now I simply have a load method which populates all the widgets with data, on page load obviously. My question is how to handle the real time aspect of further updates.

我想到只有多个ajax调用,它们可以每秒查询一次服务,并返回最新数据,但这似乎效率很低.

I thought of having just multiple ajax calls, which could query a service every second or so, and return the latest data, but that seems inefficient.

是否可以通过控制器将数据""到视图?

Is there a way to "push" data to a View from a Controller?

推荐答案

这取决于前端数据需要更新的频率.大多数页面不需要不断更新.我不知道有最佳实践"阈值,但是我认为使用Ajax更新15-20秒是一个很好的起点.使您的Ajax呼叫快速而精简-如果没有更新,则可能只返回空白.如果您需要比此更快的更新,请查看名为long polling的更新.基本上,长轮询是触发对服务器的Ajax调用的地方,并且连接一直保持打开状态,直到有数据要发送为止.长轮询将占用更多的服务器资源,因为在它们等待数据准备就绪时,您将有开放的连接和线程在运行.使用ASP.NET,您还必须担心杀死长轮询线程,因为默认情况下,当浏览器关闭连接时(例如,如果有人离开页面,这些线程将不会被杀死).

It depends on how often the data on the front end needs to be updated. Most pages aren't going to need constant updating. I don't know that there is a "best practice" threshold, but I think a good starting point would be 15-20 second updates using Ajax. Make your Ajax calls fast and lean - maybe just return blank if there are no updates. If you need faster updates than that, look into something called long polling. Long polling is basically where you trigger an ajax call to the server, and the connection sits open until there is data to be sent. Long polling will take more server resources, because you will have open connections and threads running while they are waiting for data to be ready. With ASP.NET you'll also have to worry about killing long polling threads, because by default those threads wouldn't be killed when the browser closes connection (for example if someone navigates away from the page.)

这篇关于将数据从ASP.NET MVC控制器推送到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆