Luca | LeCarbonator
Luca | LeCarbonator
Explore posts from servers
CC#
Created by Luca | LeCarbonator on 11/18/2024 in #help
✅ Binding Nested values in RequestBody to Model
I seem to have find a way to do it in the model binder:
public class FooBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(Foo))
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}

var readResult = await bindingContext.HttpContext.Request.BodyReader.ReadAsync();
var content = Encoding.UTF8.GetString(readResult.Buffer);

if (content == null)
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}

var jo = JObject.Parse(content);

// Set all properties with a [JsonProperty] attribute attached
Foo? output = jo.ToObject<Foo>();

if (output == null)
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}

output.BarProp = (string)jo.SelectToken("some.nested.property.from.json")!;

bindingContext.Result = ModelBindingResult.Success(output);
return;
}
}
public class FooBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(Foo))
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}

var readResult = await bindingContext.HttpContext.Request.BodyReader.ReadAsync();
var content = Encoding.UTF8.GetString(readResult.Buffer);

if (content == null)
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}

var jo = JObject.Parse(content);

// Set all properties with a [JsonProperty] attribute attached
Foo? output = jo.ToObject<Foo>();

if (output == null)
{
bindingContext.Result = ModelBindingResult.Failed();
return;
}

output.BarProp = (string)jo.SelectToken("some.nested.property.from.json")!;

bindingContext.Result = ModelBindingResult.Success(output);
return;
}
}
5 replies
CC#
Created by Luca | LeCarbonator on 11/18/2024 in #help
✅ Binding Nested values in RequestBody to Model
Is there another way to obtain it other than bindingContext.HttpContext.Request.BodyReader?
5 replies
CC#
Created by Luca | LeCarbonator on 11/18/2024 in #help
✅ Binding Nested values in RequestBody to Model
it enforces the read to be async, but IModelBuilder throws an error even though it's async
'CollectionBinder' does not implement interface member 'IModelBinder.BindModelAsync(ModelBindingContext)'. 'CollectionBinder.BindModelAsync(ModelBindingContext)' cannot implement 'IModelBinder.BindModelAsync(ModelBindingContext)' because it does not have the matching return type of 'Task'.
'CollectionBinder' does not implement interface member 'IModelBinder.BindModelAsync(ModelBindingContext)'. 'CollectionBinder.BindModelAsync(ModelBindingContext)' cannot implement 'IModelBinder.BindModelAsync(ModelBindingContext)' because it does not have the matching return type of 'Task'.
5 replies
CC#
Created by Luca | LeCarbonator on 11/18/2024 in #help
✅ Binding Nested values in RequestBody to Model
It looks like Model Binding is the way to go, but getting the request body's text is more difficult than I thought
5 replies
CC#
Created by Luca | LeCarbonator on 11/15/2024 in #help
Specifying 4xx Details
I‘m curious how the default formatting works if I don‘t pass anything to the function It doesn‘t seem editable on its own, but perhaps I can manually create it and then pass it as parameter
8 replies
CC#
Created by Luca | LeCarbonator on 11/15/2024 in #help
Specifying 4xx Details
maybe net9 soon if the changes are related to 4xx error handling as was mentioned before
8 replies
CC#
Created by Luca | LeCarbonator on 11/15/2024 in #help
Specifying 4xx Details
I haven‘t clarified it yet, you‘re right. My target is net8
8 replies
CC#
Created by Luca | LeCarbonator on 11/15/2024 in #help
Specifying 4xx Details
Oh, interesting! I'll take a look. Also thanks for the video reference
8 replies
CC#
Created by Luca | LeCarbonator on 11/13/2024 in #help
✅ Is it possible to access a Controller's URI at runtime?
:HmmNotes: sounds like a plan. These keywords should be enough to solve the post. Do I just mark it with :greencheck: ?
20 replies
CC#
Created by Luca | LeCarbonator on 11/13/2024 in #help
✅ Is it possible to access a Controller's URI at runtime?
thanks for the advice
20 replies
CC#
Created by Luca | LeCarbonator on 11/13/2024 in #help
✅ Is it possible to access a Controller's URI at runtime?
hmm, I see. I'll read up on the HttpContext then
20 replies
CC#
Created by Luca | LeCarbonator on 11/13/2024 in #help
✅ Is it possible to access a Controller's URI at runtime?
Either one is of help, since the href should already be a filled out URL At least I don't expect to change my endpoints constantly, so that is okay to be hardcoded for this size of a project
20 replies