No description, website, or topics provided.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
WebApplication
.gitignore
LICENSE
README.md
WebApplication.sln

README.md

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