-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Is there a way to bind the request data manually instead of being in the parameters list? #4811
Comments
@mrahhal have a look at the |
@dougbu tried it. Maybe it doesn't work with data in the request's body or something? It says it gets data from the current controller's IValueProvider so I'm not sure. Is there a demo or an example in the tests? Closest thing I'm finding on the subject is implementing my own |
@mrahhal what exactly did you try? |
The simplest case, a json post request: class TestModel
{
public string Some { get; set; }
}
[HttpPost("test")]
public async Task<IActionResult> Test()
{
var model = new TestModel();
await TryUpdateModelAsync(model); // => true
return Ok(model);
}
|
I think in this case you need to read the Request's I think that @mrahhal, out of curiosity, what is the reason you want to do this manually instead of letting MVC do the work? |
@Eilon I have an endpoint that updates a user's profile (which is another domain model that is different from Right now I'm providing one endpoint for each user kind, so: [HttpPut("~/api/users/{userName}/profile/kind1")]
public IActionResult PutProfileKind1(string userName, Kind1Model model) {...}
[HttpPut("~/api/users/{userName}/profile/kind2")]
public IActionResult PutProfileKind2(string userName, Kind2Model model) {...} I'd really like to make this into a single endpoint So the result that I'd like to be able to do is this: [HttpPut("~/api/users/{userName}/profile")]
public IActionResult PutProfile(string userName)
{
var user = GetUser(userName);
if (user.Kind == UserKind.Kind1)
{
// Something like this:
var model = new Kind1Model();
ManualBind(model);
// check ModelState.IsValid ...
Update(user, model);
}
else if (user.Kind == UserKind.Kind2)
{
var model = new Kind2Model();
ManualBind(model);
// check ModelState.IsValid ...
Update(user, model);
}
// ...
} If there is really no way to do such manual binding I can think of a lot of different scenarios where this might be really useful. Well, I can always read the request's body manually but I lose some of the binding's features ( |
@mrahhal the following should do the trick but #4652 (fixed in [FromBody]
class TestModel
{
public string Some { get; set; }
} Instead
|
@dougbu so I register |
@mrahhal please provide a complete repro project, preferably as a GitHub repo. |
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)?
The text was updated successfully, but these errors were encountered: