restrict ibnder to query, form and route
This commit is contained in:
parent
5938879b98
commit
6477b79086
|
|
@ -76,6 +76,23 @@ namespace NejCommon.Models
|
||||||
_routeValues = routeValues;
|
_routeValues = routeValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string? GetValue(ModelBindingContext context, string name)
|
||||||
|
{
|
||||||
|
// Route first
|
||||||
|
if (context.ActionContext.RouteData.Values.TryGetValue(name, out var routeValue))
|
||||||
|
return routeValue?.ToString();
|
||||||
|
|
||||||
|
// Then query string
|
||||||
|
if (context.HttpContext.Request.Query.TryGetValue(name, out var queryValue))
|
||||||
|
return queryValue.FirstOrDefault();
|
||||||
|
|
||||||
|
// Then form
|
||||||
|
if (context.HttpContext.Request.HasFormContentType && context.HttpContext.Request.Form.TryGetValue(name, out var formValue))
|
||||||
|
return formValue.FirstOrDefault();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task BindModelAsync(ModelBindingContext bindingContext)
|
public async Task BindModelAsync(ModelBindingContext bindingContext)
|
||||||
{
|
{
|
||||||
if (bindingContext == null)
|
if (bindingContext == null)
|
||||||
|
|
@ -100,17 +117,17 @@ namespace NejCommon.Models
|
||||||
? bindingContext.ModelName
|
? bindingContext.ModelName
|
||||||
: routeValue.Name;
|
: routeValue.Name;
|
||||||
|
|
||||||
var result = bindingContext.ValueProvider.GetValue(name);
|
var result = GetValue(bindingContext, name);
|
||||||
// If these really are route values, don't go through the entire
|
// If these really are route values, don't go through the entire
|
||||||
// MVC ValueProvider infrastructure.
|
// MVC ValueProvider infrastructure.
|
||||||
if (result == ValueProviderResult.None)
|
if (result == null)
|
||||||
{
|
{
|
||||||
bindingContext.ModelState.AddModelError(name, "Route value not found");
|
bindingContext.ModelState.AddModelError(name, "Route value not found");
|
||||||
bindingContext.Result = ModelBindingResult.Failed();
|
bindingContext.Result = ModelBindingResult.Failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
values[i] = result.FirstValue!;
|
values[i] = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if all the required route values are present
|
// Check if all the required route values are present
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user