c# - 如何在 ASP.NET MVC 5 Controller 中返回 http 中的字符串?

标签 c# asp.net asp.net-mvc

我正在重构一个使用外部服务提交表单的网站,一旦他们向我发送表单数据,他们就希望收到一个 http 响应字符串,让他们知道我已经收到他们的 POST。

这是以前的内容,当时网站是 web 表单/aspx。

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

所以我首先在我的 Controller 中尝试了这个:

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

    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

但这似乎不起作用。然后我尝试了:

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

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

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

还是不行。我不知道他们是否获得了 200 而没有获得“OK”字符串?

编辑:无效,我的意思是外部服务没有收到我的“OK”字符串。

最佳答案

简单如:

return Content("OK", "text/plain");

关于c# - 如何在 ASP.NET MVC 5 Controller 中返回 http 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42860120/

相关文章:

asp.net - IIS 7.5 基于域名的 URL 重写

asp.net - 保存 ASP.NET 错误而不将其显示给访问者

javascript - 将 javascript 变量转换为 C# 变量

ASP.NET MVC : Can I change my mind about having a strongly typed view?

c# - 在 C# 中不鼓励时,如何在一个方法中编写多个异常?

c# - 在 C# 中创建三级嵌套字典

c# - 包含 char 数组时快速读取 C 结构

c# - C# 对 Java Web 服务的请求中 header 和请求之间的奇怪字符会阻止成功

c# - Asp.net mvc Sp​​ecified UserAgent no session Id

c# - XML 字符串数组反序列化