ASP。NET MVC -如何处理应用程序的全局设置

时间:2022-01-21 04:12:31

Suppose each user has certain preferences that is saved in database for that user. For example: UnitSystem, UILanguage, TimeZone,...

假设每个用户都有一些为该用户保存在数据库中的首选项。例如:UnitSystem, UILanguage, TimeZone,…

When an http-request is made we need to have access to user preferences (e.g UnitSystem, TimeZone, ...) to correctly process the data and render the view in the correct language.

当http请求发出时,我们需要访问用户首选项(e)。正确处理数据并以正确的语言呈现视图。

What is the correct way to save/access user preferences during program execution?

在程序执行期间保存/访问用户首选项的正确方法是什么?

  1. Read the user preferences from database for each http-request
  2. 从数据库中读取每个http请求的用户首选项
  3. Read user preferences once when user logs into the application and save it in session variables.
  4. 当用户登录到应用程序并将其保存在会话变量中时,请读取一次用户首选项。
  5. Read user preferences once when user logs into the application and save it in a cookie.
  6. 当用户登录到应用程序并将其保存到cookie中时,请读取一次用户首选项。

How do you handle global settings in your MVC based applications?

如何在基于MVC的应用程序中处理全局设置?

2 个解决方案

#1


1  

In your case, I would probably store the setting in the session and cookie and check them in this order:

在你的情况下,我可能会将设置存储在会话和cookie中,并按以下顺序检查:

  1. Check the session for the variable.
  2. 检查该变量的会话。
  3. If not in the session, check the cookie and store cookie value in session.
  4. 如果不在会话中,请检查cookie并将cookie值存储在会话中。
  5. If not in cookie, check database and store in cookie and session.
  6. 如果不在cookie中,请检查数据库并将其存储在cookie和会话中。

That way you should be able to handle session timeouts & users with cookies turned off pretty transparently while still maximizing performance by hitting the DB only when absolutely necessary.

通过这种方式,您应该能够处理会话超时&使用cookie的用户可以非常透明地关闭,同时仍然可以在绝对必要的情况下通过点击DB来最大化性能。

Of course you'll need some mechanism to update the cookie and session as well if the user changes their preferences in the DB. Assuming these preferences are set in the same application, that shouldn't be too big of a deal.

当然,如果用户在DB中更改他们的首选项,您还需要一些机制来更新cookie和会话。假设这些首选项是在同一个应用程序中设置的,那就没什么大不了的。

#2


1  

This is what the Profile providers are for in ASP.NET. Take a look at the section of the MSDN documentation titled "ASP.NET Profile Properties Overview", located at:

这就是在ASP.NET中配置文件提供程序的用途。请看MSDN文档中名为“ASP”的部分。NET Profile Properties Overview,位于:

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

You could then create your own profile provider (if one of the provided ones doesn't suit your needs) which would load/save the profile data.

然后您可以创建您自己的概要文件提供程序(如果提供的其中一个不适合您的需要),它将加载/保存概要文件数据。

#1


1  

In your case, I would probably store the setting in the session and cookie and check them in this order:

在你的情况下,我可能会将设置存储在会话和cookie中,并按以下顺序检查:

  1. Check the session for the variable.
  2. 检查该变量的会话。
  3. If not in the session, check the cookie and store cookie value in session.
  4. 如果不在会话中,请检查cookie并将cookie值存储在会话中。
  5. If not in cookie, check database and store in cookie and session.
  6. 如果不在cookie中,请检查数据库并将其存储在cookie和会话中。

That way you should be able to handle session timeouts & users with cookies turned off pretty transparently while still maximizing performance by hitting the DB only when absolutely necessary.

通过这种方式,您应该能够处理会话超时&使用cookie的用户可以非常透明地关闭,同时仍然可以在绝对必要的情况下通过点击DB来最大化性能。

Of course you'll need some mechanism to update the cookie and session as well if the user changes their preferences in the DB. Assuming these preferences are set in the same application, that shouldn't be too big of a deal.

当然,如果用户在DB中更改他们的首选项,您还需要一些机制来更新cookie和会话。假设这些首选项是在同一个应用程序中设置的,那就没什么大不了的。

#2


1  

This is what the Profile providers are for in ASP.NET. Take a look at the section of the MSDN documentation titled "ASP.NET Profile Properties Overview", located at:

这就是在ASP.NET中配置文件提供程序的用途。请看MSDN文档中名为“ASP”的部分。NET Profile Properties Overview,位于:

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

http://msdn.microsoft.com/en-us/library/2y3fs9xs.aspx

You could then create your own profile provider (if one of the provided ones doesn't suit your needs) which would load/save the profile data.

然后您可以创建您自己的概要文件提供程序(如果提供的其中一个不适合您的需要),它将加载/保存概要文件数据。