0

I need in MCV4 web-api to return a http status to user (after calling POST/GET methods).

(New project of MCV4, and choose: WEB-API).

I have VS 2010, and I looked at the sample on Returning http status code from Web Api controller, but it didn't work for me.

I am using ApiController to handle GET/POST methods.

For following code:

[ResponseType(typeof(User))]
public HttpResponseMessage GetUser(HttpRequestMessage request, int userId, DateTime lastModifiedAtClient)
{
    var user = new DataEntities().Users.First(p => p.Id == userId);
    if (user.LastModified <= lastModifiedAtClient)
    {
         return new HttpResponseMessage(HttpStatusCode.NotModified);
    }
    return request.CreateResponse(HttpStatusCode.OK, user);
}

I put the above in class of type: apicontroller - ResponseType is unknown, and indeed, in code, I added:

using System.Web.Http;

But, still ResponseType is unknown.

I am using 32bit for my win-api.

How can I return status to user (whether it is error, or OK).

Thanks :)

Community
  • 1
  • 1
Eitan
  • 1,286
  • 2
  • 16
  • 55
  • 1
    "it didn't work" is the worst kind of problem description. Can't you tell us more about it "didn't work"ing? "ResponseType is unknown" how are you measuring this? Every http request has a status otherwise it would be malformed and the browser/client would not understand the message. Look in an http inspector such as developer tools in your browser or fiddler2 and see the status code. Perhaps your (not included) client code needs looking at? – spender Sep 08 '14 at 08:48
  • Can you give us the precise error message? Are you saying it won't compile? – Ben Robinson Sep 08 '14 at 08:52
  • It is not compiled for: ResponseType. – Eitan Sep 08 '14 at 10:47
  • I did as example on: https://www.youtube.com/watch?v=H9vBxAH4f5E. Just need to add response to user on apicontroller class. – Eitan Sep 08 '14 at 11:28

1 Answers1

1

As per MSDN document, the ResponseTypeAttribute is in the namespace "System.Web.Http.Description"

Refer: ResponseTypeAttribute

Thangadurai
  • 2,573
  • 26
  • 32
  • What shall I write in code. As the example I gave - it is not compiled. – Eitan Sep 08 '14 at 10:53
  • @Eitan, Did you check for this using clause in your code? "using System.Web.Http.Description" – Thangadurai Sep 09 '14 at 06:38
  • Hi. I have finally succeeded sending response (not using response type) - I just using System.Web.Http.Description, and declare my get function as something like public HttpResponseMessage Post([FromBody]string value) - or public HttpResponseMessage Get() ... and in body of function do call to: Request.CreateResponse correspondingly to my code behavior. You can consider this case as close. Thanks, anyway :) – Eitan Sep 09 '14 at 07:15