Custom action result not working when running in Azure

الإجابة Custom action result not working when running in Azure

  • 2012年4月11日 19:34
     
      コードあり

    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

すべての返信

  • 2012年4月12日 9:43
    モデレータ
     
     

    Hi,

    I can reproduce your situation at my side, the code works OK when set MVC application as the startup project, and if i deploy it on local IIS, it's also works. Unless set Azure application as the startup project and in Azure environment.

    Let me loop the issue to Azure team and see if they could helps.

    Appreciate your patience.

    Thank you.


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


  • 2012年4月16日 7:30
    モデレータ
     
     回答済み

    Hi,

    Please add below in web.config. Please let me know whether it works for you.

      <system.webServer>

    ...

        <httpErrors existingResponse="PassThrough"/>
      </system.webServer>


    Allen Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


  • 2012年4月17日 10:54
    モデレータ
     
     

    Hi,

    I will mark Allen's post as the Answer, if you still have any problem please feel free to unmark it.

    BR,

    Arwind


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework