Overriding the controller methods
See original GitHub issueDescription
Hello, i’ve a question (and i think there is a problem in here)
I’ve a controller named GamesController
, and i want to override the patch method to change some properties on the fly. So i’ve change the controllers content similar to this.
Controller
[Authorize]
[Route("jsonapi/[controller]")]
public class GamesController : JsonApiController<Game>
{
public GamesController(
IJsonApiContext jsonApiContext,
IResourceService<Game> resourceService,
ILoggerFactory loggerFactory)
: base(jsonApiContext, resourceService, loggerFactory)
{
}
[HttpPatch("{id}")]
public override Task<IActionResult> PatchAsync(int id, [FromBody] Game entity)
{
//Force to change some property of entity <------ question is here
entity.Title = $"{entity.Title} {DateTime.Today.ToShortDateString()}";
return base.PatchAsync(id, entity);
}
}
Sample request body
{
"data": {
"type": "games",
"attributes": {
"title": "My game"
}
}
}
Actually it’s working without giving an error, but not accepting the value change.
Summary
- Current title at the Db: “Old game”
- Sent title: “My game”
- Changed title via controller: “My game 03.03.2019” (i can see this value in the quickwatch)
- Database record after the patch operation: “My game” (this is the problem) Expected database record after the patch operation: “My game 03.03.2019”
Environment
- JsonApiDotNetCore Version: 3.1.0
- Other Relevant Package Versions:
- Net Core: 2.2.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Overriding MVC Controller When Controller In a Separate ...
However, I want to override certain Controller functions based on which project (a.k.a. "site") is calling the function.
Read more >Why and how to override Laravel's built-in auth controller ...
We can replace (or overwrite) them by creating the same method name in our class, but then we can't use parent::getLogin() to call...
Read more >Overriding the index method on a controller · Issue #320
I'm trying to display all the users that aren't administrators in my users dashboard. def index super @resources = User.where(admin: ...
Read more >Can I override controller method (not action method) in plugin
So want to override below method (not action method) in Nop plugin. i.e. where ever this method is called in Nop core code,...
Read more >How to Override an Existing Controller in Odoo 14
To override a controller, you have to create a subclass to define existing functions. Now select a function from the controller for ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry for previous post, there was mistake. For updating values there is JsonApiContext (IIJsonApiContext param of your Controller), which is holder of changed attributes from PATCH request.
You should store jsonApiContext to private variable and do this
There is the limitation comming from HttpPatch itself. HttpPatch reflects only truly changed attributes. For example, you have Game entity, you send PATCH on your title attribute, then you face several problems:
That’s the reasons why AttributesToUpdate and RelationshipsToUpdate exists. .NET Core itself uses JsonPatchDocument https://dotnetcoretutorials.com/2017/11/29/json-patch-asp-net-core/ but it’s coupled with application/json-patch+json standard and it’s quite overkill when api needs only application/vnd.api+json