gpt4 book ai didi

asp.net-mvc - ASP.NET View 中的 Response.Write 是一个坏主意吗?

转载 作者:行者123 更新时间:2023-12-04 23:10:46 25 4
gpt4 key购买 nike

我公司的大部分 Web 编程背景都是 PHP,因此 ASP.NET MVC 是我们最新的 Web 应用程序的合理选择,该应用程序还需要作为单独的 Winforms 应用程序的后端工作。然而,我们不断发现自己在走我们通常在 php 中遵循的道路 - 为条件输出回显大量标签。这是 ASP.NET MVC 中的一个坏主意吗?

例如,没有 Response.Write:

      <%if (OurUserSession.IsMultiAccount)
{%>
<%=Html.ActionLink("SwitchAccount", "Accounts", "Login") %><span>|</span>
<%}%>

使用 Response.Write:
      <%if (OurUserSession.IsMultiAccount)
Response.Write (Html.ActionLink("Swith Account", "Accounts", "Login") + "<span>|</span>");
%>

这里的区别很小,但有时我们的 View 逻辑会变得更复杂(非常复杂的 View 逻辑,即除了 bool 值之外的任何东西,我们只是卸载到 Controller )。第二个似乎更容易阅读,但我想看看是否对此事有任何想法。

最佳答案

正如 Mehrdad 所说,使用 Response.Write() 没有任何好处。与 <%= %> 相比.但是,如果您想让代码更具可读性,可以使用扩展方法:

public static string WriteIf(this HtmlHelper helper, bool condition, string tag) {
return condition ? tag : "";
}

这将像这样使用:
<%= Html.WriteIf(UserSession.IsMultiAccount,
Html.ActionLink("Swith Account", "Accounts", "Login") + "<span>|</span>") %>

我想,哪个更容易阅读是品味问题。

关于asp.net-mvc - ASP.NET View 中的 Response.Write 是一个坏主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1387546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com