Mapping json request body properties in ASP.NET Core API Controllers

Posted on 2017-10-16

FromBodyProperty

FromBodyPropertyAttribute lets you bind ASP.NET Core MVC Controller action parameters from raw request body json content properties.
[FromBody] allows you to bind a single parameter with data from the request body, with multiple values you will need to use a complex object.
[FromBodyProperty] allows you to bind multiple parameters with data from the request body:
Example controller action with multiple parameters:
public MultiplyResponse Multiply([FromBodyProperty] int x, [FromBodyProperty] int y)
The name of the action parameters are specified in the request body data:
{ "x": 2, "y": 5 }

How to use

Setup

In Startup.cs add (I)FromBodyPropertyModelBinderHelper & FromBodyPropertyJsonOptionsSetup
public void ConfigureServices(IServiceCollection services) {
	services.AddMvc();
	services.AddScoped<IFromBodyPropertyModelBinderHelper, FromBodyPropertyModelBinderHelper>();
	services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, FromBodyPropertyJsonOptionsSetup>());
}

Example controller

public class HomeController : Controller {
	public MultiplyResponse Multiply([FromBodyProperty] int x, [FromBodyProperty] int y) {
		return new MultiplyResponse() { Result = x * y };
	}
}

Example ajax request

var request = {
	x: 2,
	y: 5
}

var ajaxOptions = {
	url: "/Home/Multiply",
	dataType: "json",
	contentType: "application/json; charset=utf-8",
	type: "POST",
	data: JSON.stringify(request),
	success: function (data, textStatus, jqXHR) {
		alert(JSON.stringify(data, null, 4));
	},
	error: function (jqXHR, textStatus, errorThrown) {
		if (jqXHR.responseJSON) {
			alert(JSON.stringify(jqXHR.responseJSON, null, 4));
		}
		alert(textStatus);
	}
}
jQuery.ajax(ajaxOptions);

Demo project & code

https://github.com/IonKiwi/FromBodyProperty

Samsung Galaxy S5+ | G901F | kccat6xx

Posted on 2016-09-11

lz4 for .NET

Posted on 2015-01-01
lz4 for .NET, original C sources recompiled for the CLR (/clr:pure)
https://github.com/IonKiwi/lz4.net

Currently supports:
  • non streaming, using managed byte arrays
  • streaming, uses the LZ4 Framing Format (v1.4.1)

Example

non-streamed
  // compress data
  byte[] dataToCompress = ...
  byte[] compressedData = LZ4Helper.Compress(dataToCompress);
  // decompress data
  byte[] decompressedData = LZ4Helper.Decompress(compressedData);


streamed
  // compress data [with content checksum]
  using (LZ4Stream stream = LZ4Stream.CreateCompressor(innerStream, LZ4FrameBlockMode.Linked, LZ4FrameBlockSize.Max64KB, LZ4FrameChecksumMode.Content, -1, false)) {
    // write uncompressed data to the lz4 stream
    // the stream will compress the data and write it to the innerStream
    stream.Write(buffer, 0, buffer.Length); 
  }

  // compress data [with block and content checksum, start a new frame after 100 data blocks]
  using (LZ4Stream stream = LZ4Stream.CreateCompressor(innerStream, LZ4FrameBlockMode.Linked, LZ4FrameBlockSize.Max64KB, LZ4FrameChecksumMode.Block | LZ4FrameChecksumMode.Content, 100, false)) {
    // write uncompressed data to the lz4 stream
    // the stream will compress the data and write it to the innerStream
    stream.Write(buffer, 0, buffer.Length); 
  }

  // decompress data
  using (LZ4Stream stream = LZ4Stream.CreateDecompressor(innerStream, false)) {
    // the lz4 stream will read the compressed data from the innerStream
    // and return the uncompressed data in 'buffer'
    int bytesRead = stream.Read(buffer, 0, buffer.Length)
  }

Break on exception extension

Posted on 2013-11-19
An extension to automatically continue execution when an exception occurs at a certain point/location.

Breaking when an exception is thrown can be very useful.
You can configure this using the well-known exception dialog:
http://msdn.microsoft.com/en-us/library/d14azbfh(v=vs.120).aspx


exception settings


In some cases though you don’t want to stop every time when an exception is thrown. For instance when ‘Enable Just My Code’ is disabled and you are using some kind of third party library that throws some generic (first chance) exception.

So this extension is for those cases: automatically resume execution when an exception is thrown at a specific location without pressing F5 or pressing continue in the exception dialog/popup.

To configure the extension:
Tools -> Options -> Break on Exception Helper

Add the specific locations to automatically continue execution by specifying:
  • The exception type (namespace + typename)
  • (example: System.ArgumentException)
  • The name of the function that throws the exception
  • (example: TestFirstChance.TestClassB.TestA)
  •  
  • Optionally:
  • The second stack frame number that needs to match
  • The second stack frame function name

Download links below.

Coming soon

Posted on 2012-03-11
Blog will be live soon!
new Blog().Launch()
© 2018 - Ewout van der Linden - IonKiwi.nl