René
René

Reputation: 73

Write to file using code in Composite C1 in Azure?

I've tried searching the web for a couple of days now, trying to figure out a way to solve this.

I have Composite C1 .NET CMS installed as a Web App in Azure, and I want to cache the results of an external web service call on the server in Composite. The results are returned as JSON, so my idea is to write the JSON results to a JSON or text file on the server (it's public content, so no privacy issues there) along with a time stamp, and read the JSON from that file if the time stamp is newer than x minutes ago. If not, I will call the web service for new results and overwrite the existing content. Reading/loading works perfectly fine when I manually copy the results into a file on the server and read from it, but my problem is with writing to said file using File.WriteAllLines().

I've made it as a Razor function, but whenever I try to write to a file (even with relative path), I get an error telling me I (obviously) don't have permission to access D:\Windows\System32. This is due to the nature of Azure I believe. The only option I've found so far is using the Form Builder to simulate a file upload, but that seems just too much for my intention.

Am I going about this the wrong way? My intention is to avoid using a database or BLOB storage for such a simple operation, when it works so well when I manually upload a file through Composite's administration/system management and read from it.

Thanks in advance.

Upvotes: 1

Views: 89

Answers (1)

René
René

Reputation: 73

Disregard this question. As soon as I posted it, I had an "aha"-moment and found the Server.MapPath() metod. I guess I had misunderstood how Azure Web App storage worked.

My final solution was simply this:

private void WriteJsonToFile(string jsonContent) {
    string[] content = { DateTime.Now.ToString(), jsonContent };

    System.IO.File.WriteAllLines(Server.MapPath("~/JSON/feed.txt"), content);
}

Upvotes: 1

Related Questions