I am trying to set a status code in a custom action result and it does not work.
Please note that the sample bellow works great in Casini or IIS but does not in the DevFabric or Azure.
Here is my custom action result:
/// <summary>
/// Custom file stream action result
/// </summary>
public sealed class CustomFileStreamResult : FileStreamResult
{
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Web.Mvc.CustomFileStreamResult" /> class.
/// </summary>
/// <param name="statusCode">the status code to return</param>
/// <param name="fileStream">The stream to send to the response.</param>
/// <param name="contentType">The content type to use for the response.</param>
/// <exception cref="T:System.ArgumentNullException">
/// The <paramref name="fileStream" /> parameter is null.
/// </exception>
public CustomFileStreamResult(int statusCode, Stream fileStream, string contentType)
: base(fileStream, contentType)
{
this.StatusCode = statusCode;
}
/// <summary>
/// Gets the status code to return
/// </summary>
public int StatusCode { get; private set; }
/// <summary>
/// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult" /> class.
/// </summary>
/// <param name="context">The context within which the result is executed.</param>
/// <exception cref="T:System.ArgumentNullException">
/// The <paramref name="context" /> parameter is null.
/// </exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "This is an override of an existing .Net method")]
public override void ExecuteResult(ControllerContext context)
{
base.ExecuteResult(context);
context.HttpContext.Response.StatusCode = this.StatusCode;
}
}
Here is my action code:
public class AccountController : Controller
{
public ActionResult Index()
{
int status = 400;
string path = @"file.txt";
Stream stream = System.IO.File.OpenRead(path);
string contentType = "application/json";
return new CustomFileStreamResult(status, stream, contentType);
}
}
Whenever setting the status to 200, the client code (see bellow) will get the file as the content of the stream.
When setting the status to 400, the body is set to "Bad Request".
When setting the status to 500, the body is set to the default 500 error page.
Here is the test client code:
Uri uri = new Uri(path2);
HttpWebRequest req = HttpWebRequest.Create(uri) as HttpWebRequest;
req.Method = "GET";
req.ContentType = "application/json";
req.Accept = "application/json";
req.ContentLength = 0;
Stream stream = null;
try
{
var res = req.GetResponse();
stream = res.GetResponseStream();
}
catch (WebException webEx)
{
Console.WriteLine("EXCPETION");
HttpWebResponse exceptionResponse = webEx.Response as HttpWebResponse;
HttpStatusCode exceptionStatusCode = HttpStatusCode.ServiceUnavailable;
if (exceptionResponse != null)
{
exceptionStatusCode = exceptionResponse.StatusCode;
}
if (webEx.Status == WebExceptionStatus.ProtocolError &&
webEx.Response != null)
{
stream = webEx.Response.GetResponseStream();
}
}
StreamReader sr = new StreamReader(stream);
Console.WriteLine(sr.ReadToEnd());
Any help would be appreciated.
Thanks