I have a very simple Web API with only GET controller. Inside the get controller, I am reading an XML file, converting that XML file into a JSON object and returning it.
public JObject Get()
{
//Read XML
XDocument xDoc = XDocument.Load(@"D:\myfile.xml");
string jsonStr = JsonConvert.SerializeXNode(xDoc);
JObject json = JObject.Parse(jsonStr);
return json;
}
I want to put the XML reading and JSON conversion lines into an exception handler and return 404 Error code (or any proper code for this situation) if the XML file cannot be opened (or any other exception occurs). But since my return type id JObject
, I am not able to return a proper http respose, so how can I do that?