Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 

FromBodyProperty

FromBodyPropertyAttribute lets you bind ASP.NET Core MVC Controller action parameters from raw request body json content properties.

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);

License

Apache 2.0

About

No description, website, or topics provided.

Resources

License

Releases

No releases published

Packages

No packages published