MVC视图| *你如何*处理IsAuthenticated方案?

时间:2021-01-20 23:22:24

Typically, in sites that allow membership, you want to offer your users a bit of data that is only visible when they're logged in.

通常,在允许成员身份的站点中,您希望为用户提供一些仅在登录时才可见的数据。

For my site, if the logged in user is the data owner, I want to offer some tools to let them manage the data.

对于我的网站,如果登录用户是数据所有者,我想提供一些工具来让他们管理数据。

My question is this. Do you split this duty between two different views? One view that gets loaded for "regular" users, the other that gets loaded for "owner" users. The view that the regular users see simply shows the data. The owner sees the data and some tools to manage it.

我的问题是这个。你是否在两种不同观点之间分配了这个职责?一个为“常规”用户加载的视图,另一个为“所有者”用户加载的视图。常规用户看到的视图只显示数据。所有者会看到数据和一些管理它的工具。

Or, do you perform checks in a single view and hide/show blocks within it (similar to what you would have done in regular ASP.NET)?

或者,您是否在单个视图中执行检查并隐藏/显示其中的块(类似于您在常规ASP.NET中的操作)?

It's probably by preference, but are there any technical reasons for splitting the duty between two views vs a single view?

这可能是偏好,但是有没有任何技术原因可以将两个视图之间的任务与单个视图分开?

4 个解决方案

#1


I would also go for the single view option as well. Would provide specific properties on your viewdata to indicate what to do.

我也会选择单视图选项。将在您的viewdata上提供特定属性以指示要执行的操作。

<% if (Model.IsOwner) { %>
    //Html for owner
<% } %>

#2


Personally I would go for the single view option. That way you don't have to repeat code that'll appear in both views.

我个人会选择单视图选项。这样您就不必重复出现在两个视图中的代码。

Technically (or in regards to the MVC pattern) I can't think of any reasons to split it up.

技术上(或关于MVC模式)我想不出有任何理由将其拆分。

#3


I would be inclined to split the view into multiples as ideally you want to avoid conditional logic in a view (read: ideally).

我倾向于将视图分成多个,因为理想情况下你想要在视图中避免条件逻辑(理想情况下读取)。

If you find that would cause some duplication between your views, then it's likely that you could move that duplicated content into shared partials.

如果您发现这会导致视图之间出现重复,那么您可能会将重复的内容移动到共享的部分内容中。

#4


I usually go for rendering the extra content in a partial, with conditional logic in the partial checking for roles prior to rendering:

我通常会在部分中使用条件逻辑来渲染额外内容,在渲染之前部分检查角色:

<%-- Master Page --%>
<% Html.RenderPartial("DataOwnerStuff"); %>

<%-- Partial --%>
<% if(Roles.IsUserInRole("DataOwner")) { %>
    <h1>Hi Data Owner!</h1>
<% } %>

#1


I would also go for the single view option as well. Would provide specific properties on your viewdata to indicate what to do.

我也会选择单视图选项。将在您的viewdata上提供特定属性以指示要执行的操作。

<% if (Model.IsOwner) { %>
    //Html for owner
<% } %>

#2


Personally I would go for the single view option. That way you don't have to repeat code that'll appear in both views.

我个人会选择单视图选项。这样您就不必重复出现在两个视图中的代码。

Technically (or in regards to the MVC pattern) I can't think of any reasons to split it up.

技术上(或关于MVC模式)我想不出有任何理由将其拆分。

#3


I would be inclined to split the view into multiples as ideally you want to avoid conditional logic in a view (read: ideally).

我倾向于将视图分成多个,因为理想情况下你想要在视图中避免条件逻辑(理想情况下读取)。

If you find that would cause some duplication between your views, then it's likely that you could move that duplicated content into shared partials.

如果您发现这会导致视图之间出现重复,那么您可能会将重复的内容移动到共享的部分内容中。

#4


I usually go for rendering the extra content in a partial, with conditional logic in the partial checking for roles prior to rendering:

我通常会在部分中使用条件逻辑来渲染额外内容,在渲染之前部分检查角色:

<%-- Master Page --%>
<% Html.RenderPartial("DataOwnerStuff"); %>

<%-- Partial --%>
<% if(Roles.IsUserInRole("DataOwner")) { %>
    <h1>Hi Data Owner!</h1>
<% } %>