Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.net web api post action parameters
    primarykey
    data
    text
    <p>I'm currently writing a little application consisting of a mvc web api server and mvc 4 client, I've faced a problem, that is eating my mind for several days already.</p> <p>I have a ContactsController, that contains:</p> <pre><code>[HttpPost] public ActionResult AddContact(ContactModel model, HttpPostedFileBase image) { return ActionWrapper(() =&gt; { if (model.InitializeFromCookie()) { if (image != null) { model.ImageMimeType = image.ContentType; model.Picture = new byte[image.ContentLength]; image.InputStream.Read(model.Picture, 0, image.ContentLength); } PostObject("api/contacts/postcontact", model); RefreshCookie(); return RedirectToAction("Contacts", "Contacts"); } else { return JsonRedirectResult(Url.Action("Login", "Users")); } }); } </code></pre> <p>Model is derived from AuthorizationContent. The post object method:</p> <pre><code>[NonAction] protected object PostObject(string apiUrl, object obj, object additionalData = null) { var query = additionalData != null ? additionalData.GetQueryString() : string.Empty; apiUrl += query; var action = _client.PostAsJsonAsync(apiUrl, obj); action.Wait(); if (!action.Result.IsSuccessStatusCode) { var code = action.Result.StatusCode; var error = action.Result.ReasonPhrase; throw new ServerSideException(code, error); } else { return action.Result.Content.ReadAsAsync&lt;object&gt;().Result; } } </code></pre> <p>The method is about to call webapi controllers' method:</p> <pre><code>[AuthorizedOnly, HttpPost] public void PostContact([FromBody]AuthorizationContent item, User authorizedUser) { var realItem = Mapper.Map&lt;ContactModel, Contact&gt;((ContactModel) item); _contactService.AddContact(authorizedUser, realItem); } </code></pre> <p>The filter:</p> <pre><code>[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] class AuthorizedOnly : ActionFilterAttribute { private ISessionService _sessionService; private Session _userSession; public ISessionService SessionService { get { return _sessionService ?? (_sessionService = NinjectWebCommon.Kernel.Get&lt;ISessionService&gt;()); } } public override void OnActionExecuting(HttpActionContext actionContext) { var auth = actionContext.ActionArguments.Values.FirstOrDefault() as AuthorizationContent; if (auth == null) { throw new UnauthorizedException(); } else { var user = SessionService.GetMemberBySessionCredentials(auth.Token, auth.UserName); _userSession = user.Session; if (SessionService.IsSessionValid(_userSession)) { actionContext.ActionArguments["authorizedUser"] = user; base.OnActionExecuting(actionContext); } else { throw new UnauthorizedException(); } } } </code></pre> <p>The code works just fine for get actions, but when i am trying to do the post as shown above i'm always getting a server side exception stating </p> <blockquote> <p>{StatusCode: 400, ReasonPhrase: </p> <p>'Can't bind multiple parameters ('item' and 'authorizedUser') to the request's content.', Version: 1.1, Content: System.Net.Http.StringContent, Headers: { Content-Type: text/plain; charset=utf-8 } }</p> </blockquote> <p>The question - how can i state that authorizedUser will come from filter and model binder should NOT look for it in requests content? Sorry for my bad english and thank you beforehand!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload