admin管理员组

文章数量:1287517

I have a controller with a method which should only output static JSON content to the browser. E.g.

public async Task<ActionResult> Test()
{
    return Content("{ \"key\": \"value\" }", "application/json");
}

This works fine when I debug locally via Visual Studio, however, after deployed on IIS the response is just blank.

So feels like some configuration on the IIS that needs to be done?

Does anyone have any idea?

I have a controller with a method which should only output static JSON content to the browser. E.g.

public async Task<ActionResult> Test()
{
    return Content("{ \"key\": \"value\" }", "application/json");
}

This works fine when I debug locally via Visual Studio, however, after deployed on IIS the response is just blank.

So feels like some configuration on the IIS that needs to be done?

Does anyone have any idea?

Share Improve this question edited Feb 24 at 14:12 Uwe Keim 40.7k61 gold badges188 silver badges303 bronze badges asked Feb 24 at 9:28 NikksterNikkster 3372 silver badges13 bronze badges 4
  • 1 (1) Is this .NET Framework or .NET Core v3/5/6/7/8/9 ? (2) Do you have logging (or something else) that shows whether your endpoint is actually getting called or not? (3) The proper way to do this is not to output text yourself, but to return an object, the most simple case being return new { key = "value" }; (or use a Model class or DTO class) and letting the MVC API Controller machinery do the serializing + setting the response type. – Peter B Commented Feb 24 at 9:55
  • If you put a static index.html file there, does it serve it up correctly? If not - start there. Get that index file working, then work outwards. – mjwills Commented Feb 24 at 10:03
  • Hi @Nikkster, The test code you shared can only work well in asp core template, for this kind of issue, you should check the network tab in browser developer tool, we need to check this http request details. – Jason Pan Commented Feb 25 at 5:57
  • For backend, please enable IIS Log and failed request tracing feature, we may found something useful. And you also can share the modules installed with us, we can help you check it. – Jason Pan Commented Feb 25 at 5:58
Add a comment  | 

2 Answers 2

Reset to default 1

Did you try using JsonResult instead?

public async Task<JsonResult> Test()
{
    return new JsonResult()
                 {
                     Data = new { key = "value" },
                     JsonRequestBehavior = JsonRequestBehavior.AllowGet
                 };
}

I managed to solve this by adding the following to <system.webServer> tag in Web.config;

  <httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough">
  </httpErrors>

本文标签: cReturn content is quotblankquot when deployed on productionStack Overflow