如何实现条件get?
我正在尝试在我的控制器中实现条件获取。几天前我问了一个类似的问题,但几乎没有得到任何答案:在控制器中处理 HTTP 标头和状态代码 我想也许我没有正确解释自己,所以我正在尝试新的尝试。所以就是这样。从前端我正在做一个长轮询解决方案,我定期获取数据。数据以 json 格式出现。在我的控制器中,我返回结果集。我想做的是检查请求标头,这样如果自上次获取后没有修改,我将不会再次获取数据。这是我到目前为止所得到的:
public ActionResult Index()
{
var ifModifiedSince = Request.Headers["If-Modified-Since"];
if( !String.IsNullOrEmpty( ifModifiedSince ) )
{
Response.StatusCode = 304;
Response.StatusDescription = "304 Not Modified";
Response.End();
return new EmptyResult(); <--- not sure what I should return here.
}
else
{
return View("Index");
}
}
请记住,我已经改变了几次,而且我很累,所以这里可能有一些明显的错误。我正在从数据库中获取数据。我可能会说数据显示在谷歌地图解决方案上。那么有人有什么建议吗?感谢您的指点。
问候
I'm trying to implement an conditional get in my controller. I asked a similar question a few days ago but did hardly get any answers on that one: Handle HTTP-Headers and status codes in controller
I thought that maybe I did not explain myself correctly so I'm giving at an new try. So here it is. From the front end I'm doing a long poll solution where I fetch data with an regular interval. The data comes in the format of json. In my controller I'm returning the resultset. What I would like to do is to check the request headers so that if not modified since last fetch I wont get the data again. This is what I got so far:
public ActionResult Index()
{
var ifModifiedSince = Request.Headers["If-Modified-Since"];
if( !String.IsNullOrEmpty( ifModifiedSince ) )
{
Response.StatusCode = 304;
Response.StatusDescription = "304 Not Modified";
Response.End();
return new EmptyResult(); <--- not sure what I should return here.
}
else
{
return View("Index");
}
}
Bare in mind that this I have changed several times and I'm quite tired so there migth be some obvious errors here. I am fetching the data from a database. I might ad that the data is displayed on a google map solution. So does anyone have any suggestions?? Thankful for any pointers.
Regards
如果您正在寻求进行长轮询解决方案,则有几个类似的问题。
ASP.NET 的 Comet 实现?
普遍的共识是扩展可能很棘手。
还有这个实现示例
https://bitbucket.org/jacob4u2/mvcchatsite/src
There are a couple of similar questions to this if you are looking to do a long poll solution.
Comet implementation for ASP.NET?
The general consenus is that it can be tricky to scale.
There is also this implementation example
https://bitbucket.org/jacob4u2/mvcchatsite/src