locked
Adding Custom Header to Response. RRS feed

  • Question

  • User-1426872249 posted

    Hi,

    Whenever my application receives a request for .mp3, .3gp, .rm i want to add a custom header to the response. I want to achieve this without using any Handler. is there any chances? If yes, wat are the possible chances?

    Wednesday, May 13, 2009 7:51 AM

Answers

  • User-318989783 posted

     Well its pretty hard to  do this without handlers but you could look IIS redirects at http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/6b855a7a-0884-4508-ba95-079f38c77017.mspx?mfr=true

      or

    ASP.NET Routing       http://msdn.microsoft.com/en-us/library/cc668201.aspx

    Once you figure out the type of file in your request you can use the following code to add the header to the response. See below sample

                       Response.ContentType="application/ms-word";

              Response.AddHeader"content-disposition","attachment; filename=download.doc");
                    
              
    FileStream sourceFile = new FileStream(@"F:downloadexample.doc"FileMode.Open);
              
    long FileSize;
              
    FileSize sourceFile.Length;
              
    byte[] getContent = new byte[(int)FileSize];
              
    sourceFile.Read(getContent0, (int)sourceFile.Length);
              
    sourceFile.Close();

              
    Response.BinaryWrite(getContent);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2009 9:06 AM
  • User-318989783 posted

     You could look at writing your own ISAPI filters.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 14, 2009 10:43 PM

All replies

  • User-1659704165 posted

     Hi,

    Below Sample Shows the way Adding header in Asp.net page

    HttpContext.Current.Response.AddHeader "blah", "blah"
        Response.Write HttpContext.Current.Request.ServerVariables("HTTP_blah")

    HttpContext.Current.Response.ContentType = "application/msword";

    Wednesday, May 13, 2009 8:30 AM
  • User-1426872249 posted

    Hi, thnx for the response. im not able to understand your code. can you please more clear and explain?

    Wednesday, May 13, 2009 8:34 AM
  • User-1426872249 posted

    hi, if i request a file like http://www.somesite.com/foldername/movie.3gp it will not come to Application_BeginRequest if im not wrong. then how can i add header to it.

    Wednesday, May 13, 2009 8:37 AM
  • User-1659704165 posted

     
    //The Above Will Create Header
    HttpContext.Current.Response.AddHeader "<Name Of Header>", "<Message Here>"
    //Below code Will retrive  Information From Headr
    HttpContext.Current.Request.ServerVariables("HTTP_<Name Of Header>")
    //U may Add Content Type in header
    HttpContext.Current.Response.ContentType = "application/.p3";

    Wednesday, May 13, 2009 8:42 AM
  • User-318989783 posted

     Well its pretty hard to  do this without handlers but you could look IIS redirects at http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/6b855a7a-0884-4508-ba95-079f38c77017.mspx?mfr=true

      or

    ASP.NET Routing       http://msdn.microsoft.com/en-us/library/cc668201.aspx

    Once you figure out the type of file in your request you can use the following code to add the header to the response. See below sample

                       Response.ContentType="application/ms-word";

              Response.AddHeader"content-disposition","attachment; filename=download.doc");
                    
              
    FileStream sourceFile = new FileStream(@"F:downloadexample.doc"FileMode.Open);
              
    long FileSize;
              
    FileSize sourceFile.Length;
              
    byte[] getContent = new byte[(int)FileSize];
              
    sourceFile.Read(getContent0, (int)sourceFile.Length);
              
    sourceFile.Close();

              
    Response.BinaryWrite(getContent);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2009 9:06 AM
  • User-1426872249 posted

    Hi infosolvex,

    is there any alternate way instead of using iis redirect / asp.net routing? In IIS 6.0 and .net 1.1

    Thursday, May 14, 2009 6:15 AM
  • User-318989783 posted

     You could look at writing your own ISAPI filters.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 14, 2009 10:43 PM