possible to return a http request from inside an actionResult?

Using an ActionResult: if everything goes well return new EmptyResult if something goes wrong throw an exception. However as not all code paths return a value I need to add some sort of return in at the bottom of the function. Preferably an http request...404 maybe.

What im trying to do is Run this function from a console app ad download the PLU records to a CSV file. This all works fine, but throws an error with the 'return'....any ideas welcome

public ActionResult GetPLUInfo()
    {
        try
        {
            //CSV FILE of PLUs
            IEnumerable<PLURecord> listOfPLUs = _pluService.GetAll();

            using (StreamWriter outfile = new StreamWriter(@"C:\Users\Martin\Desktop\chocListWRITE.csv", true)) //true:  append text to a file with StreamWriter. The file is not erased, but just reopened and new text is added to the end. 
            {
                foreach (PLURecord _plu in listOfPLUs)
                {
                    string line = _plu.Serialize();
                    outfile.WriteLine(line);
                    Console.WriteLine(line);
                }

            }

            HttpContext.ApplicationInstance.CompleteRequest();



          return new EmptyResult();
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetPLUInfo.");
                Console.WriteLine(ex.Message);
            }

            //RETURN: How can i say here to return a http request error,
            //Using the ActionResult
        }

Answers


You can set the Response.StatusCode to a valid http error like:

Response.StatusCode = 500;

Then you can let your error handling in the web.config handle what to do, redirect to a custom action of your own or just return the page.


Source So

As per this post you can return like this

return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);

You can just try this

catch (Exception)
        {
            throw;
        }

Need Your Help

Why android toolbar height is cut?

android android-layout android-studio toolbar

I am new to the design pattern in latest android studio,the one with include

Image resize canvas

jquery css canvas

I'm trying to upload images and have them fit into different sized boxes....To give you an idea of what the application does: People upload images and have them printed onto posters.