Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal API (with EF Scaffolder) should be updated to handle PUT requests correctly #2270

Closed
vijayrkn opened this issue Feb 9, 2023 · 2 comments

Comments

@vijayrkn
Copy link
Contributor

vijayrkn commented Feb 9, 2023

Issue:

The current scaffolded code first tries to find the resouce (in a tracking way) and uses a different model to update the resource. Since the model updated is different from the model added to the context, the PUT operation fails.

Code block with the issue:

https://github.com/dotnet/Scaffolding/blob/main/src/Scaffolding/VS.Web.CG.Mvc/Templates/MinimalApi/MinimalApiEf.cshtml#L98-L110

Fix

There are 2 ways to fix this issue:

  1. The find operation needs to change to a non-tracking form:
    model.AsNoTracking().FirstOrDefaultAsync(r => r.Id == Id)

  2. Based on https://www.ietf.org/rfc/rfc2616.txt, the PUT operation is not required to check for the existence of the resource. If the provided model matches an existing resource it will be updated, else a new resource should be created.

   If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

Preference:

Since the API controller scaffolders follow 2, it would be preferable to match the behavior for both API end-point and API controller scaffolders.

Thanks @timheuer for identifying and tracking down the issue.

@DamianEdwards - Do you have a preference on how these should be updated to handle the error scenario here?

Related issue: #1964

@deepchoudhery
Copy link
Member

#2269 addresses a lot of the concerns but does not address If the provided model matches an existing resource it will be updated, else a new resource should be created. in the fix suggested above. Any thoughts on that @DamianEdwards @sayedihashimi.

@vijayrkn
Copy link
Contributor Author

Closing this since it is addressed with the new EF model of update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants