Webservice throw 404

  • Thread starter Thread starter Oscar
  • Start date Start date
O

Oscar

Hi,

I got a webservie that supply users with binary files based on a parameter
id. The method is void and I stream the file by:

HttpContext.Current.Response.ContentType = "application/xxx";
HttpContext.Current.Response.BinaryWrite(arrResult);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
return;

If the parameter id is invalid or if something goes wrong I would like to
throw either 404 och 500 exception. I been tring to do this with the
following code but I will allways recieve a 500. How can I control the
output exception.
 
I forgot to past some code....


HttpContext.Current.Response.StatusCode = 500;
throw new HttpException(500, "Error getting the file.\n\n" + e);
 
Oscar,

Well, you are passing back a 500. In what case do you hope to send back
a 404? I don't see anything that indicates that you want to send back a 404
response.
 
Hi,

Sorry, I pasted the wrong code... it should of cource be:

HttpContext.Current.Response.StatusCode = 404;
throw new HttpException(404, "Error getting the file.\n\n" + e);

//Oscar


Nicholas Paldino said:
Oscar,

Well, you are passing back a 500. In what case do you hope to send
back a 404? I don't see anything that indicates that you want to send
back a 404 response.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oscar said:
I forgot to past some code....


HttpContext.Current.Response.StatusCode = 500;
throw new HttpException(500, "Error getting the file.\n\n" + e);
 
Oscar,

You shouldn't have to set the StatusCode if you are throwing the
HttpException. Are you finding that just throwing the exception is not
returning a 404 response?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oscar said:
Hi,

Sorry, I pasted the wrong code... it should of cource be:

HttpContext.Current.Response.StatusCode = 404;
throw new HttpException(404, "Error getting the file.\n\n" + e);

//Oscar


Nicholas Paldino said:
Oscar,

Well, you are passing back a 500. In what case do you hope to send
back a 404? I don't see anything that indicates that you want to send
back a 404 response.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oscar said:
I forgot to past some code....


HttpContext.Current.Response.StatusCode = 500;
throw new HttpException(500, "Error getting the file.\n\n" + e);



Hi,

I got a webservie that supply users with binary files based on a
parameter id. The method is void and I stream the file by:

HttpContext.Current.Response.ContentType = "application/xxx";
HttpContext.Current.Response.BinaryWrite(arrResult);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
HttpContext.Current.Response.Close();
return;

If the parameter id is invalid or if something goes wrong I would like
to throw either 404 och 500 exception. I been tring to do this with the
following code but I will allways recieve a 500. How can I control the
output exception.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top