是否可以通过对Web API的URL调用获取实时数据? [英] Is it possible to get real-time data via a URL call to a Web API?

查看:55
本文介绍了是否可以通过对Web API的URL调用获取实时数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个ASP.NET MVC 4 Web API项目.您的资源之一,当您通过URL调用时,会等待它在指定的时间内获取性能监视数据,然后在完成后以JSON形式全部返回.但是,在输入URL与完成过程之间,有一种方法可以动态地返回数据,即.检索性能数据并在浏览器中将其每秒显示一次.

Let's say you have an ASP.NET MVC 4 Web API project. One of your resources, when you call it by URL, waits while it gets performance monitoring data for a specified amount of time and then returns it all in JSON form once it has completed. However, between entering the URL and when the process has completed, is there a way to return data dynamically, ie. at each second when performance data is retrieved and display it in the browser.

这是问题所在:据我所知,并且据所有人所知,通过URL调用API资源是静态的.意思是,直到资源检索到所有信息,JSON才会出现,这不是我想要的.我希望能够在API资源正在检索数据的同时,在浏览器中不断更新JSON.

Here's the issue: Calling an API resource via URL is static as far as I know and as far as anyone seems to know. Meaning, the JSON won't appear until the resource has retrieved all of its information, which is not what I want. I want to be able to constantly update the JSON in the browser WHILE the API resource is retrieving data.

由于我正在使用存储库类和控制器类,因此无法使用Javascript.我已经尝试过使用SignalR,但显然在这种情况下不起作用,尤其是因为我无法使用Javascript.

Since I'm working in a repository class and a controller class, Javascript is not an option. I've tried using SignalR, but apparently that does not work in this scenario, especially since I'm not able to use Javascript.

是否可以通过对API的URL调用来获取实时数据?

Is there any possible way to get real-time data with a URL call to the API?

要点:谷歌地图.通过URL调用Google Maps API的唯一方法是,如果您需要一个静态"地图,该地图显示特定位置的单个图像.没有任何形式的互动.如果需要动态的实时"地图,则需要构建一个Web应用程序,并在视图页面中使用Javascript消耗应用程序中的API资源.无法通过URL调用它.

Case in point: Google Maps. The only way you can call the Google Maps API via URL is if you want a "static" map, that displays a single image of a specific location. No interaction of any kind. If you want a dynamic, "real time" map, you need to build a web application and consume the API resource in your application with Javascript in a view page. There is no way to call it via URL.

推荐答案

您可以将老式的ASP.Net IHttpHandler实现放在一起,而不管MVC控制器和路由管道如何. http://support.microsoft.com/kb/308001 .然后,您将对响应流有完整的访问权限,并且可以根据自己的意愿缓冲或不缓冲.不过,您必须考虑是否要长时间捆绑工作线程,并且如果您打算或多或少地连续流式传输,那么您一定要使用IAsyncHttpHandler来等待来自存储库的进一步响应.

You can put together an old-school ASP.Net IHttpHandler implementation regardless of MVC controllers and routing pipeleline. http://support.microsoft.com/kb/308001. You would then have full acesss to the response stream and you could buffer or not as you see fit. You've got to consider though whether you want to tie up worker thread for that long and if you're planning on streaming more or less continuously then you definately want to use IAsyncHttpHandler while you await further responses from your repo.

话虽如此,Web API也支持Async,但它稍微复杂一些.如果您打算随时发送回数据,那么我强烈建议您再看一看SignalR,如果您打算最终拥有一些JavaScript客户端,那么它可以立即完成所有这些工作.这要容易得多.

Having said that, Web API supports Async too but it's a little more sophisticated. If you plan on sending back data as-and-when, then I'd strongly recommend you take another look at SignalR which does all this out of the box if you are planning on having some JavaScript client side eventually. It's much, much easier.

但是,如果您确实想在Web API中编写异步内容,那么这里有一些资源可能对您有所帮助;

If you really want to write Async stuff in Web API though, here's a couple of resources that may help you;

这看起来确实正是您所需要的; http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx

And this one looks like exactly what you need; http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx

但是,为了在示例中使用该PushStreamContent()类,您不会在System.Net.Http.dll中找到它,您需要从每晚在 http://aspnetwebstack.codeplex.com/SourceControl/list/changesets

In order to use that PushStreamContent() class in the example though, you'll not find that in your System.Net.Http.dll, you'll need to go get that from the Web API stack nightly builds at http://aspnetwebstack.codeplex.com/SourceControl/list/changesets

YMMV-祝你好运!

YMMV - good luck!

这篇关于是否可以通过对Web API的URL调用获取实时数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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