C# list to anonymous type with new properties

This is my expected anonymous type out of a list object;

var roleData = new[] {
    new { roleName = "Office Director"},
    new { roleName = "Manager Operations"},
    new { roleName = "Training Manager" }
};

This is how we can create anonymous type out of a strongly typed C# list;

foreach (UserLocation location in request.RoleRequest.UserLocations)
{
   ....................
   var rolesData = location.Roles
               .Select(x => new { roleName = x.RoleName }).ToArray();  
}

For more examples, continue on Stack Overflow

URL Matching in C#

To start, let’s define what the internals of a URL looks like:

For our purposes, we care about the scheme, authority, path, query, and fragment. You can think of the scheme as the protocol, i.e., HTTP or HTTPS. The authority is the root or domain, for example, mycompany.com. The path, query, and fragment make up the rest of the URL. The URL spec defines each segment in this specific order. For example, the scheme always comes before the authority. The path comes after the scheme and authority. The query and fragment come after the path if there is one in the URL.

Read this, if thinking about using fragment in URL.

Alice runs a web site, Bob visits it, authenticates and receives a session cookie. (Some time might pass here, Bob might even close his browser.) Charlie sends Bob a mail saying “check out this cool link!”. Bob opens the link, which leads to a site controlled by Charlie. The page redirects Bob’s browser to a page on Alice’s site with an attack payload in the hash. The payload is executed, and since the browser still remembers the cookies, it can just send them to Charlie

https://stackoverflow.com/questions/1822598/getting-url-hash-location-and-using-it-in-jquery

Read more here

Possible values of empty json

An empty string is not a valid json;

string json = "";

While an empty string is not valid JSON two quotes is valid JSON. This is an important distinction.

Which is to say a string that contains two quotes is not the same thing as an empty string.

string json = "{}";
string json = "[]";

Valid minimal JSON strings are

The empty object '{}'

The empty array '[]'

The string that is empty '""'

A number e.g. '123.4'

The boolean value true 'true'

The boolean value false 'false'

The null value 'null'

Resource

https://stackoverflow.com/questions/30621802/why-does-json-parse-fail-with-the-empty-string

Convert Json String to C# object

Let’s say we have this json string in Ajax post method;

let groupObject = '[{ "Key": "Commercial", "IsMember": "true" }, { "Key": "Corporate", "IsMember": "false" }, { "Key": "Consumer", "IsMember": "false" }]';

Make sure this is a valid json. We can use following web site for json validation;

https://jsonlint.com/

We will create a user group class in .NET;

public class UGroupVM
{
    public string? Key { get; set; }
    public string? IsMember { get; set; }
}

We will user JSON Deserialization to get group object (try to use IList or List otherwise deserialization will fail because json is formatted as key/value pair);

var userGroup = JsonSerializer.Deserialize<List<UserGroupVM>>(groupObject);

For Newtonsoft, this is the syntax

var userAdGroup =
 JsonConvert.DeserializeObject<List<UserGroupVM>>(groupObject);

An alternative is this;

JArray jarray = JArray.Parse(groupObject);

foreach (JObject jObject in jArray)
{
    Console.WriteLine($"{(string)jObject["Key"]} -> {(string)jObject["IsMember"]}");
}