Home > Mobile >  Restful API C# Reading from .Txt File
Restful API C# Reading from .Txt File

Time:07-23

I am trying to build my first ever API and I want it to read some text from a .txt file.

I wrote the code originally to have the text be stored in memory and then remove it after the HTTP Get request. I had it working properly when I would run it in Visual Studio, so I decided to deploy it to Azure. I would then make the HTTP request to add more text into the memory and then I would essentially request it back, however I wouldn't get the text I wanted back. Instead I would get my error message saying that there is nothing in the list which holds the text I want.

I then decided to have it write the text to a .txt file then receive it from that. Once again this worked on my side, but once I published the code to Azure and send a request, I get a 500 Internal Server Error.

At the end of this long story is my question since I can't seem to find any details about .txt files on an Azure Web server.

Is it possible to have my C# class's in my Models folder of my API Web app read a .txt file on an Azure server?

CodePudding user response:

Is it possible to have my C# class's in my Models folder of my API Web app read a .txt file on an Azure server?

The short answer: yes.

The slightly longer answer: yes, however...

As posted in the comments reading and writing information to and from a file in a web application can go bad really fast. Think about two users trying to write at the same time, one user reading at the moment another one is writing and so on.

Now I can imagine the only thing you want to do right now is test to see if it works, so multiple users might not be on top of mind. But even a proof of concept deserves the correct approach, since you might learn something the wrong way otherwise.

Have a look at storing your information in a data store inside Azure, there are quite a few options. For this just to work, it's probably best to use something that's not too hard to set up like Table Storage. If you run into any issues trying to get this to work, open up a new question and we'll be happy to help.

CodePudding user response:


500 Internal Server Error, these is general error in server side. I reproduce same problem and its working fine by using following steps.

After deploy on Azure its show 500 error.

enter image description here

you find actual error to enable Application Insights and go to Failures tab. You can see actual error why occurred. enter image description here Click on Error enter image description here

See Error and File location. enter image description here

Check File location, if file not there location then create a file on that location. Go to advance tool enter image description here

Click to go link. enter image description here

Select Cmd from Debug console dropdown. and click on Site->wwwroot enter image description here Create a txt file enter image description here

Output : Now its working.

enter image description here

  • Related