3

I am trying to create API calls to read file from azure data lake store. However I am unable to show proper response and error for specific file found and not found respectively.

I am able to connect with azure data lake and fetch data as well, used try-catch properly. Please help me with proper response body and response codes for c# APIs.

try
        {
            string aa = GetItemInfo("/myfolder/subfolder/testfile.txt");
            return new string[]
            {
            "Hello",
            aa,
            "World"

        };
            }
catch {
            return new string[]
         {
            "Hello",
            "World"

     };

My code is working. As I am new with API calls on C# I am unable to figure out to correct method to do so.

2 Answers 2

3

You have to create the following

var responseMessage = new HttpResponseMessage>(errors, HttpStatusCode.BadRequest);

throw new HttpResponseException(responseMessage);

you can find the answer here and also here

Sign up to request clarification or add additional context in comments.

Comments

2

There are many reasons which will cause an exception. For example, network issue or token is expired. To show proper response and error for specific file found and not found, I suggest you check the path is exist or not before read the item information.

public static  bool ItemExist(string path)
{
    return _adlsFileSystemClient.FileSystem.PathExists(accountName, path);
}

Code which use ItemExist method to check whether the path is exist.

string path = "/myfolder/subfolder/testfile.txt";
if (ItemExist(path))
{
    string aa = GetItemInfo(path);
    return new string[]
    {
        "Hello",
        aa,
        "World"
    };
}
else
{
    var responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
    responseMessage.Content = new StringContent("The file path which you requested is not found");
    throw new HttpResponseException(responseMessage);
}

For other unexpected exceptions, I suggest you use Global Error Handling in your ASP.NET Web API.

5 Comments

Thankyou for the bool function, that worked. Also, please help me out how to copy a file from one folder to another using c# on azure
Thankyou for the link, but that one is for moving data. I just want to copy same data from one folder to another.
I am sorry to tell that copy method is not implemented in C# SDK currently. I suggest you do the copy action by downloading file and uploading the file.
Oh okay. Thank-you for the help over this. I will do download and upload then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.