in my WebApi controller post function I assign user id to my posted object through User.identity.GetUserId()
but ModelState.isValid
always return validation error that
user id is required
while the value is assigned to the required property. So I removed the required attribute from userId
property. It is working but I need to know as to why ModelState
does not know that the user id has already been provided with User.Identity.GetUserId()
.
If you are using Web API 2, instead of User.identity.GetUserId()
use
RequestContext.Principal.Identity.GetUserId()
or
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
Request.GetOwinContext().GetUserManager<ApplicationUserManager>()