Writing custom 500 response body works with IIS Express, but fails on IIS ASP.Net Core 1.0
Date : March 29 2020, 07:55 AM
I hope this helps . As Kiran mentions above, I was not setting the content length. This solved the problem. Now both of these solutions work fine on IIS: string msg = Newtonsoft.Json.JsonConvert.SerializeObject(new { message = message, incidentId = incidentId });
byte[] bytes = Encoding.UTF8.GetBytes(msg);
context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
context.HttpContext.Request.ContentLength = bytes.Length;
await context.HttpContext.Response.Body.WriteAsync(bytes, 0, bytes.Length);
var result = new JsonResult(new { message = message, incidentId = incidentId });
result.StatusCode = StatusCodes.Status500InternalServerError;
context.Result = result;
|
Custom model binding through body in ASP.Net Core
Date : March 29 2020, 07:55 AM
it helps some times I found a solution. The body of a HTTP Post request using ASP.NET Core can be obtained in a custom model binder using this lines of code string json;
using (var reader = new StreamReader(bindingContext.ActionContext.HttpContext.Request.Body, Encoding.UTF8))
json = reader.ReadToEnd();
|
How to do custom model binding in Asp.Net Core MVC
Date : March 29 2020, 07:55 AM
I wish did fix the issue. In this scenario, you don't need to create your own binder. The built-in model binder has already done it for you. So the easiest way is to let the ASP.NET Core bind the model automatically:
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task SignIn(
[ModelBinder(typeof(SignInRequestModelBinder))]
[FromForm]SignInRequestModel request, // the [FromForm] is required when you're using an ApiController
string returnUrl = null
)
{
...
} public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null) { throw new ArgumentNullException(nameof(bindingContext)); }
var request = bindingContext.ActionContext.HttpContext.Request;
var model = new SignInRequestModel();
var pis = typeof(SignInRequestModel).GetProperties();
foreach(var pi in pis){
var pv = bindingContext.ValueProvider.GetValue(pi.Name); // prop value
pi.SetValue(model,pv.FirstValue);
}
bindingContext.Result = ModelBindingResult.Success(model);
return Task.CompletedTask;
}
|
Custom Model Binding in Asp .Net Core
Date : March 29 2020, 07:55 AM
|
ASP.NET Core customize the error when model binding fails
Date : March 29 2020, 07:55 AM
wish of those help You can customize your error message from Startup class in ConfigureServices method. You can see details Microsoft document. Here is an example - services.AddMvc(options =>
{
var iStrFactory = services.BuildServiceProvider().GetService<IStringLocalizerFactory>();
var L = iStrFactory.Create("ModelBindingMessages", "WebUI"); // Resource file location
options.ModelBindingMessageProvider.SetValueIsInvalidAccessor((x) => L["The value '{0}' is invalid."]);
options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor((x) => L["The field {0} must be a number."]);
options.ModelBindingMessageProvider.SetMissingBindRequiredValueAccessor((x) => L["A value for the '{0}' property was not provided.", x]);
options.ModelBindingMessageProvider.SetAttemptedValueIsInvalidAccessor((x, y) => L["The value '{0}' is not valid for {1}.", x, y]);
options.ModelBindingMessageProvider.SetMissingKeyOrValueAccessor(() => L["A value is required."]);
options.ModelBindingMessageProvider.SetUnknownValueIsInvalidAccessor((x) => L["The supplied value is invalid for {0}.", x]);
options.ModelBindingMessageProvider.SetValueMustBeANumberAccessor((x) => L["Null value is invalid.", x]);
});
|