4

I have a ASP.NET Web API, and I have been responding to request with this format,

    [HttpPost]
    [Route("")]
    public HttpResponseMessage AlexaSkill()
    {
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
        response.Content = new StringContent("put json here", Encoding.UTF8);
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); 

        return response;
    } 

and that has been working great. The issue is that there are certain situation where the requester does not expect a response. I cannot figure out how to not give a response to the requester who is posting to the url. How can I be able to return a response like a have above and also have the option to have the function not give a respons essentially acting as a void function?

3 Answers 3

4

You should always return a response. There's a status code 204 for when you don't want to send content in your response. From the spec:

10.2.5 204 No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

So your code could be something like this:

[HttpPost]
public HttpResponseMessage SomeMethod()
{
    // Do things
    return Request.CreateResponse(HttpStatusCode.NoContent);
} 
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for you answer. I just tried that, but the application that is sending the request has an error because it receives a non 200 HTTP status code. The documentation for the application that I am trying to comply with says, "Your service cannot send back a response to a SessionEndedRequest." That is why I am wondering if there is some way to sometimes not return anything from my controller.
Ah, I see. Well, if the caller was a properly-behaving client this would be a good answer, but it looks like you have to do something a bit different.
Added an alternate answer. It's not clear what's meant by "cannot send back a response," but if that means to just end the response, it might work for you.
The real problem is that I don't know how to interrupt "cannot send back a response" as well. I thought your answer below would work, but the application throws another error saying there was a problem communicating with my error. This leads me to believe that the application is actually expecting a response of some sort. I think I'll have to ask around on the applications developer forum to get clarifications about what the protocol is. Thanks for you help.
I agree. SessionEndedRequest probably has some sort of specific meaning. Good luck.
4

Even a void method will return an HTTP status code to the client invoking the API. See this link

You'll probably need to ask for changes or another alternative to your client.

Comments

1

If you want to just terminate the request, try this:

HttpContext.Current.Response.End();
throw new Exception("Terminating request.");

It seems like a strange thing for an HTTP server to do, but if that's what you really need, give that a shot. If you follow by throwing an exception, then an error won't be sent to the client because you've already ended the response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.