ASP.NET Core v2 (2015) MVC : How to get raw JSON bound to a string without a type?

The cleanest option I’ve found is adding your own simple InputFormatter: public class RawJsonBodyInputFormatter : InputFormatter { public RawJsonBodyInputFormatter() { this.SupportedMediaTypes.Add(“application/json”); } public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { var request = context.HttpContext.Request; using (var reader = new StreamReader(request.Body)) { var content = await reader.ReadToEndAsync(); return await InputFormatterResult.SuccessAsync(content); } } protected override bool CanReadType(Type type) … Read more

How can I get the name of a variable passed into a function?

What you want isn’t possible directly but you can use Expressions in C# 3.0: public void ExampleFunction(Expression<Func<string, string>> f) { Console.WriteLine((f.Body as MemberExpression).Member.Name); } ExampleFunction(x => WhatIsMyName); Note that this relies on unspecified behaviour and while it does work in Microsoft’s current C# and VB compilers, and in Mono’s C# compiler, there’s no guarantee that … Read more

Returning a reference to a local variable in C++

This code snippet: int& func1() { int i; i = 1; return i; } will not work because you’re returning an alias (a reference) to an object with a lifetime limited to the scope of the function call. That means once func1() returns, int i dies, making the reference returned from the function worthless because … Read more