如何使我的 Web 应用程序无状态但仍然做一些有用的事情? [英] How do I make my Web Application stateless yet still do something useful?

查看:15
本文介绍了如何使我的 Web 应用程序无状态但仍然做一些有用的事情?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这个建议...

理想情况下,Web 应遵循 REST 原则并且完全无状态.因此,单个 URL 应标识单个资源,而不必保留每个用户的导航历史记录.

ideally the web should follow the REST principle and be completely stateless. Therefore a single URL should identify a single resource, without having to keep the navigation history of each user.

...我阅读了维基百科页面 http://en.wikipedia.org/wiki/REST 听起来确实不错,但我不知道如何实际实现它.我正在使用 ASP .NET Webforms 而不是 MVC.

...and I read the Wikipedia page http://en.wikipedia.org/wiki/REST and it really sounds good, but I don't get how to actually implement it. I'm working in ASP .NET Webforms NOT MVC.

例如,在我即将构建的应用程序中 - 我需要我的用户登录,然后才能允许他们做任何事情.在允许他们做很多有用的事情之前,他们必须跳过几道坎——比如接受 T 和 C 并确认他们的基本细节没有改变.终于允许他们做他们真正想做的事情,比如 BuyAProduct!

For example, in the application I am about to build - I need my user to Login before I allow them to do anything. There are a couple of hoops they have to jump through before they are allowed to do much useful - like Accept T's and C's and confirm their basic details are unchanged. Finally they are allowed to do something they really want like BuyAProduct!

在我看来(我来自富客户端的有状态的世界)我需要状态来记录他们所做的事情并从中推断出他们被允许做的事情.我不知道我如何支持他们(比如)为 BuyAProduct URI 添加书签.当他们到达书签时,我如何知道他们是否已登录、他们是否同意 T 和 C 以及他们是否尽职尽责地检查了他们的基本详细信息?

It seems to me (I come from the HEAVILY stateful world of the Rich client) that I need state to record what they have done and infer from that what they are allowed to do. I don't see how I can support them (say) bookmarking the BuyAProduct URI. When they arrive at the bookmark how do I know if they have logged in and if they agreed to the T's and C's and if they dutifully checked their basic details?

我喜欢应用程序无状态的想法,部分原因是它似乎完全解决了当用户单击后退和前进按钮时我该怎么办?"的问题.我不知道如何才能让它正常工作.我觉得我在这方面缺少一些非常基本的东西.

I love the idea of the app being stateless, partly because it seems to completely solve the problem of "What the heck do I do when the user clicks on the Back and Forward buttons?" I don't see how I can still get it to work properly. I feel I am missing something really fundamental about this.

推荐答案

该建议并不是建议 app 应该是无状态的 - 而是建议应用中的资源应该是无状态的.也就是说,一个名为www.mysite.com/resources/123"的页面将始终代表相同的资源,无论哪个用户正在访问它,也不管他们是否登录.

The advice isn't suggesting that the app should be stateless - it's suggesting that the resources in the app should be stateless. That is, a page called "www.mysite.com/resources/123" will always represent the same resource, regardless of which user is accessing it or whether they're logged in or not.

(您可能拒绝未登录的用户访问这一事实是一个单独的问题 - 重点是 Uri 本身不依赖于特定于用户的数据才能工作.)

(The fact that you might deny a non-logged-in user access is a separate issue - the point is that the Uri itself doesn't rely on user-specific data to work.)

例如,违反此规则的网站类型是您导航到产品页面,通过电子邮件将 Uri 发送给您的朋友,然后点击它时,他们会看到一条消息,内容为对不起,您的会话已过期"或此产品不存在"或类似内容.发生这种情况的原因是因为 Uri 包含了特定于网站上用户会话的内容,如果其他用户尝试使用该链接(或稍后使用同一用户),则该链接不再有效.

For example, the kind of sites that break this rule are those where you navigate to a product page, email the Uri to your friend, and on clicking it they see a message along the lines of "I'm sorry, your session has expired" or "This product does not exist" or similar. The reason this happens is because the Uri includes something specific to the user's session on the site, and if a different user tries to use the link (or the same user at a later time), it's no longer valid.

因此,您的应用程序始终仍需要某种形式的状态,但该状态在何处实现是重要因素.

So, you will always still need some form of state for your application, but where that state is implemented is the important factor.

希望能有所帮助!

这篇关于如何使我的 Web 应用程序无状态但仍然做一些有用的事情?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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