Is Response.Write in ASP.NET submission a bad idea?

Most of my web background for web programming is PHP, so ASP.NET MVC was the logical choice for our latest web application, which also needed to work as a background addition to a standalone Winforms application. However, we continue to find ourselves along the road that we usually follow in php - repeating a lot of tags for conditional output. Is this a bad idea in ASP.NET MVC?

For example, without Response.Write:

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

With Response.Write:

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

The difference here is quite insignificant, but sometimes our presentation logic becomes more complex (a very complex presentation logic, that is, something more than logical, we just unload the controller). The second seems easier to read, but I wanted to see if there were any thoughts on this.

+4
source share
4 answers

As Mehrdad says, there is no downside to using Response.Write() compared to <%= %> . However, if you want to make your code even more readable, this may be possible using the extension method:

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

What will be used as follows:

 <%= Html.WriteIf(UserSession.IsMultiAccount, Html.ActionLink("Swith Account", "Accounts", "Login") + "<span>|</span>") %> 

Which one is easier to read, I think, is a matter of taste.

+5
source

Nope. This is not a bad idea. Functionally, these fragments are equivalent. Go with the most read in your particular case.

+2
source

<% = is exactly abbreviated for Response.Write - these two statements are not just functionally equivalent, they are identical.

The only difference is readability and brevity, for which <% = is great for everyone who has been in ASP for a while. The important thing with R.Write in general is that you avoid writing an HTML string literal with it because it is very prone to human error.

+2
source

There are two aspects to this issue:

  • Performance
  • readability / maintenance

1. Performance

Back to ASP classic days, every time you closed %> and reopened <% , there was some processing cost to the script (but processing power was equally limited). With that in mind, if we are talking about a foreach with lots of elements, I could lean towards simply using Response.Write .

2. Readability

Instead of other opinions, I personally believe that Response.Write is fairly readable. I think that the new generation of encoders does not like this simply because they remind them of the classic version.

I am still very reminiscent of Thomas Licken . It seems to be the best of two worlds.

Every time I encode any language , I try not to deviate from the Guido Python PEP-8 Style Guide , but sometimes it comes across .net C #.

After thoughts

Although this begs the question:

Is it better to call a method created specifically for %> <%= ?

0
source

All Articles