How do I restrict which properties are bound using aspnetcore JSON from body of request - asp.net-core

I could using [Bind("properties to include")] on an MVC action in ASPNET MVC 4. How do I restrict binding to specific properties using AspNetCore/MVC6? (RC2)

You could use JSON.NET's JsonIgnoreAttribute on properties which you want to exclude from deserialization, but note that this also makes the property not to serialize also. (Ideally you should have a model which is exactly what you expect from the user instead of ignoring certain properties, but I am not aware of how your requirements are)

Related

Sulu: how to make custom entity translatable?

So I have my custom entity type, created it by following official tutorial:
https://docs.sulu.io/en/2.2/book/extend-admin.html
However entity I got is not translatable like i.e. standard pages or articles. I also didn't any info on how to make it translatable. Expected behavior is just to work as those standard types.
How to achieve that?
There are basically three things to do:
You have to add a new Translation entity for your custom entity. So if you have an Event entity, you need an additional EventTranslation entity. See https://github.com/sulu/sulu-workshop/tree/master/src/Entity
You need to tell Sulu, that your custom entity is translatable by adding the available locales to the view in your AppAdmin class, see https://github.com/sulu/sulu-workshop/blob/master/src/Admin/EventAdmin.php#L74
You need to adjust your custom entity's admin controller (it will receive a locale request parameter now) to persist the localized properties to the CustomEntityTranslation instead of the CustomEntity iself, see https://github.com/sulu/sulu-workshop/blob/master/src/Controller/Admin/EventController.php
So as conclusion, Sulu is only responsible for showing the locale switcher in the upper right corner and appending the current selected locale as locale parameter to your api calls. Everything else is completely up to you, you have to implement that like in a normal symfony application

ASP.NET Core 3.0 - Model Binding not inferred?

According to the documentation, complex types should automatically be handled as "FromBody" without the need to decorate the parameter with an attribute. For whatever reason it isn't working for me. What am I missing? Thanks.
ASP.NET Core 3, Content Type of the Header is "application/json".
From the Microsoft documentation
The [ApiController] attribute applies inference rules for the default data sources of action parameters. These rules save you from having to identify binding sources manually by applying attributes to the action parameters.
The complex types could automatically be handled as "FromBody" when you use the [ApiController] attribute on the controller .

Specifying properties returned from API in client

I want to limit the amount of properties that are returned from my REST api built in dotnet core. When accessing resources the client only needs specific subsets of the data returned from the api. What is a good way to tell the api which properties the client wants returned?
My initial thought would be to add query parameters to the endpoint like this:
http://www.restapi.com/v1/resource?fields=id,name,type
But I am not sure the best way to implement this in the api so that it is reusable and clean.
You're right that you usually don't want to return your full domain model or data model over your web API. Usually you define a custom model type for this purpose, which you can also decorate with attributes for model binding and model validation, if desired.
If you want the client to be able to determine what properties it gets, you can return an anonymous type built for this purpose, or perhaps have several DTO types predefined that the client's parameters result in.

Bind the request data to a model manually instead of it being in the action's parameters list

I have an action that handles different kinds (but similar) requests. So I need the request data to bind to different models depending on several external inputs.
Is there a way to do that (so that the model isn't in the action's parameters list but bound manually)?
Implementing your own IActionModelConvention realisation, you can change the parameter binding rules.
Article, that may help: Customising model-binding conventions in ASP.NET Core
Here and here are examples in MVC github repo.

What is an analog of request.Properties in ASP.NET 5

In WebApi2 it was common to put arbitrary objects into HttRequestMessage.Properties. Usually it was doing with extension-methods like request.SetUserRights() where SetUserRights just put an object into request.Properties[HttpPropertyKey.UserRights].
Now in ASP.NET 5 there is no such property in HttpRequest.
What pattern is supposed to be be used for passing arbitrary objects along with http request?
In WebApi for putting objects in request.Properties filters were used usually. We still have filters in AspNet5, so the question can be rephrased as: where should a filter put common data specific for the request.
Examples of such data can be: current user's roles, current user's language and so on.
HttpContext class has Items property which you can use for this purpose.