Entropie,  Tech

Razor is fun…ny

Microsoft Asp.Net Razor is a sort of “PHP” markup language for Microsoft Web Pages(Views, Partial Views etc etc).  For sure better than Asp Classic or Aspx


As the special language char is @ does not require explicit closing of the code-block you can find very tricky situation (escaping CSS with a double @@ is the easiest one)

For example inside a loop, with a custom template when you need the correct relative url path given by the server (using the “~” operator) you have to write something like this:

<div class=”custom-item iconNav”>
<img class=”lang-image” src=’~/Content/icons/@(“”)<%= ImageSrc %>’) />
<div class=”lang-description”><%= Description %></div>

Where in particular:
<img class=”lang-image” src=’~/Content/icons/@(“”)<%= ImageSrc %>’) />

The <%= ImageSrc %> is related to a property inside a given loop of items (Object.ImageSrc)

The <%= %> is the aspx syntax required also in this case.

The joined @(“”) is totally an empty dummy string that call the Razor engine in order to manage the string as a dynamic one. Otherwise the final src would become

<img class=”lang-image” src=”/Content/icons/&lt;%= ImageSrc %&gt;” )=””>

instead of the correct one:

<img class=”lang-image” src=”/Content/icons/ita.svg” )=””>

Utilizzando il sito, accetti l'utilizzo dei cookie da parte nostra. maggiori informazioni

Questo sito utilizza i cookie per fornire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o cliccando su "Accetta" permetti il loro utilizzo.