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

Passing a vector/array from unmanaged C++ to C#

As long as the managed code does not resize the vector, you can access the buffer and pass it as a pointer with vector.data() (for C++0x) or &vector[0]. This results in a zero-copy system. Example C++ API: #define EXPORT extern “C” __declspec(dllexport) typedef intptr_t ItemListHandle; EXPORT bool GenerateItems(ItemListHandle* hItems, double** itemsFound, int* itemCount) { auto … Read more

Async ShowDialog

It’s easy to implement with Task.Yield, like below (WinForms, no exception handling for simplicity). It’s important to understand how the execution flow jumps over to a new nested message loop here (that of the modal dialog) and then goes back to the original message loop (that’s what await progressFormTask is for): namespace WinFormsApp { internal … Read more

How to clear browser cache on browser back button click in MVC4?

The problem with your approach is that you are setting it where it is already too late for MVC to apply it. The following three lines of your code should be put in the method that shows the view (consequently the page) that you do not want to show. Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1)); Response.Cache.SetNoStore(); If you want … Read more