Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a string in http in ASP.NET MVC 5 controller?

I'm refactoring a website using an external service for a form submission, and once they send me the form data, they expect a string of in http response to let them know I've received their POST.

This was what's there previously, when the website was in web forms/aspx.

 Response.ContentType = "text/plain";
 Response.Output.Write("OK");
 Response.Output.Flush();
 Response.Output.Close();

So I tried this first in my controller:

public ActionResult Index()
{
    //...get the form data...

    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

But this didn't seem to work. Then I tried:

public ActionResult Index()
{
    //...get the form data...

    Response.StatusCode = 200;
    Response.StatusDescription = "OK";

    return new HttpStatusCodeResult(HttpStatusCode.OK, "OK");
}

And it still didn't work. I don't know if they get the 200 and didn't get the "OK" string?

EDIT: By didn't work, I meant that the external service didn't receive my string of "OK".

like image 608
Darren Avatar asked Feb 05 '23 20:02

Darren


1 Answers

Simple as:

return Content("OK", "text/plain");
like image 145
Duncan Smart Avatar answered Mar 03 '23 19:03

Duncan Smart