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
}