ASP.NET MVC-用Web应用程序页面范围依赖依赖注入

发布于 2025-02-12 02:07:08 字数 519 浏览 1 评论 0原文

在我的ASP.NET MVC项目中,我目前正在设置一个带有巨大搜索组件的页面,可以让您搜索/过滤产品列表。

每当您单击过滤器时,就会向服务器提出HTTP请求以获取新的过滤数据(此数据位于云中)。

使用我当前的解决方案,每当您过滤某些内容时,就会提出HTTP请求,但需要知道该特定页面上这些过滤器的上一个状态。为了实现这一目标,我创建了一个带有范围寿命的缓存类,并将其注入控制器。

起初对我来说似乎很明显,但是现在我不确定它是如何工作的,它困扰着我。另外,找不到有关此特定用例的文档。

问题

每个页面请求应创建此范围的类别,这很明显。但是,在进一步的Ajax呼叫中如何持续存在?难道不应该将其视为不同的请求并因此创建一个新实例吗?

我认为控制器不绑定到首页请求,是吗?

我当前的行为是:

  • 每页请求创建类,并且在其他AJAX调用期间还活着。

有人可以向我解释吗?这是标准行为吗?还是因为它仅在我本地的机器中,所以工作正常?

In my ASP.NET MVC project, I am currently setting up a page with huge a search component that let's you search/filter a list of products.

Whenever you click on a filter, an HTTP request is made to the server to get the new filtered data (this data is located in the cloud).

With my current solution, whenever you filter something, an HTTP request is made but the BE needs to know the previous state of those filters on that specific page. In order to achieve that, I created a Cache class with SCOPED lifetime and it is injected into the controller.

At first it seemed obvious to me but now I am not so sure how it works and it's bothering me. Also, couldn't find documentation about this specific use case.

Question

This scoped class should be created per page request, that's clear. However, how come it persists in further AJAX calls? Shouldn't it be seen as a different request and therefore create a new instance?

I don't think the Controller is binds to the first page request, does it?

My current behavior is:

  • Class is created per page request and is alive during further AJAX calls.

Can someone explain this to me? Is this the standard behavior? Or is this working fine because it's only in my local machine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文