我从ActionResult继承了一个类,然后重写了ExecuteResult方法。
public class CustomResult : ActionResult
{
public object Result { get; set; }
public int StatusCode { get; private set; }
public CustomResult(int statusCode, object Result)
{
this.Result = Result;
this.StatusCode = statusCode;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
context.HttpContext.Response.StatusCode = StatusCode;
if (StatusDescription != null)
context.HttpContext.Response.StatusDescription = StatusDescription;
HttpResponseBase Response = context.HttpContext.Response;
Response.Charset = "UTF-8";
Response.ContentType = "application/json";
context.HttpContext.Response.Write(JsonConvert.SerializeObject(this));
}
}
当我从控制器返回一个CustomResult对象时,返回的Content-Type到视图总是" text / html"而不是" application / json"在CustomResult类上设置。
如果我在本地运行此应用程序一切正常,但当我将此部署到Azure App Service时,Content-Type始终为text / html。