ASP.NET Web API Conditionally Return HttpResponseMessage

I have an ASP.NET web API and I am responding to a request in 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 it works great. The problem is that there is a certain situation when the requester does not expect a response. I cannot figure out how to not respond to a request that sends a url. How can I get an answer similar to the above, and also be able to have a function that does not give a function acting like a void function?

+4
source share
3 answers

You should always return a response. There is a status code 204if you do not want to send content in your response. From the specification:

10.2.5 204 No maintenance

, . , , .

, , . , , , , .

204 .

, :

[HttpPost]
public HttpResponseMessage SomeMethod()
{
    // Do things
    return Request.CreateResponse(HttpStatusCode.NoContent);
} 
+4

void HTTP , API. .

, , .

+4

If you just want to complete the request, try the following:

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

Seems odd for an HTTP server, but if you really need it, do it. If you throw an exception, the error will not be sent to the client because you have already completed the response.

+1
source

All Articles