Wizmann
Wizmann

Reputation: 859

Azure storage account throws HTTP 400 error when writing

I've developed a simple Azure Webapp with C#. And use the table of Azure storage account to save some kinds of records of the website.

It works well on my own computer on both read and write operations, with the real connection string. I can find the record with the "Azure Storage Explorer".

However, after I deploy the app. The cloud version keeps throwing Http 400 error when I try to write the table. But the read operation still works fine. (The local version is always OK.)

enter image description here

The problem is wierd. Please help me with it.

Thanks.

== UPDATE ==

The save & query code is something like this.

public class CodeSnippet: TableEntity
{
    public string Title { get; set; }
    public string Author { get; set; }
    public string FileType { get; set; }
    public string Code { get; set; }

    public Guid Save()
    {
        var guid = Guid.NewGuid();
        var datetime = DateTimeOffset.UtcNow;

        this.RowKey = guid.ToString();
        this.PartitionKey = datetime.ToString();

        var json = JsonConvert.SerializeObject(this);

        var client = AzureStorage.DefaultClient.Instance.GetTableClient();
        var table = client.GetTableReference("privateshare");
        var insertOp = TableOperation.Insert(this);

        table.Execute(insertOp);
        return guid;
    }

    public static CodeSnippet Query(Guid? guid)
    {
        if (guid == null)
        {
            return null;
        }
        var client = AzureStorage.DefaultClient.Instance.GetTableClient();
        var table = client.GetTableReference("privateshare");

        var query = new TableQuery<CodeSnippet>()
            .Where(TableQuery.GenerateFilterCondition(
                "RowKey", QueryComparisons.Equal, guid.ToString()))
            .Take(1);
        var res = table.ExecuteQuery(query);
        return res?.First();
    }
}

Upvotes: 0

Views: 172

Answers (1)

Wizmann
Wizmann

Reputation: 859

Problem solved.

The error occurs when I take DateTimeOffset.UtcNow.ToString() as the partition key.

After I change it to DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(), it eventually works fine.

I don't know how & why the problem happens. But I guess the special characters in the "PartitionKey" may be the reason of the problem.

Upvotes: 1

Related Questions