Skip to main content
Home  ›  Blog

AsList(...) replaces many AsDynamic(...) in 2sxc 10.20

AsList(some-list) replaces AsDynamic(some-list) in 2sxc Razor and WebApi. This will make your code shorter and more readable. Discover the new AsList(...)!

Why a new AsList(...) API

Previously 2sxc used AsDynamic(...) for

  • the default lists usually like var links = AsDynamic(Data["Default"]);
  • lists like var personList = AsDynamic(Data["Persons"]);
  • single items like var person = AsDynamic(personList.First());

The first issue was with LINQ in Razor, which is dynamically typed. The main issue was that LINQ commands like .First() or .Select(...) need to know that they are being attached to an IEnumerable<...>. Since AsDynamic(...) might return a single item or a list, the compiler would complain in the above example personList.First(). In the end you would write something like (personList as IEnumerable<dynamic>).First() - which was just ugly. 

The second issue was that there were a LOT of AsDynamic(Data["Default"]) which will now just be a simpler AsList(Data)

How to get AsList(...)

We want to motivate people to use the RazorComponent base class for all new Razor work, slowly deprecating the older implementation. So to benefit from AsList, you'll need to make your razor file a RazorComponent. Once you make the switch, previous AsDynamic(list) won't work any more. Single-item use like AsDynamic(item) will continue to work.

Note that you need 2sxc 10.20.06 :)

Daniel Mettler grew up in the jungles of Indonesia and is founder and CEO of 2sic internet solutions in Switzerland and Liechtenstein, an 20-head web specialist with over 800 DNN projects since 1999. He is also chief architect of 2sxc (see github), an open source module for creating attractive content and DNN Apps.

Read more posts by Daniel Mettler