5

My ASP.NET Core API generates a SAS for a file to be uploaded to Azure Blob Storage. Looks like the string is being wrapped in double quotes and this is creating a problem with the front end solution I'm using to upload files.

How can I return a string but make sure that it's not wrapped in double quotes?

This is the API controller:

public async Task<IActionResult> GetSAS(string blobUri, string _method)
{
    if (string.IsNullOrEmpty(blobUri) || string.IsNullOrEmpty(_method))
       return new StatusCodeResult(400);

    // Get SAS
    var sas = _fileServices.GetSAS(blobUri, _method);

    return Ok(sas);
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
Sam
  • 26,817
  • 58
  • 206
  • 383
  • Show us the code you have now, we can't help otherwise. – DavidG Dec 28 '17 at 15:17
  • What does your controller method look like? ASP.NET core shouldn't wrap in double quotes by default but it sounds like your response is getting formatted as JSON somehow. – Pace Dec 28 '17 at 15:18
  • What is the type of `sas`? string? – DavidG Dec 28 '17 at 15:21
  • Just posted the API method. At the top of my API controller, I do have `[Produces("application/json")]` which explains why it's a `JSON` – Sam Dec 28 '17 at 15:21
  • @DavidG Yes, SAS is string but my API controller is producing JSON – Sam Dec 28 '17 at 15:22
  • This is the only method that is supposed to return `string` in my controller. How can I override `[Produces("application/json")]` only for that method? – Sam Dec 28 '17 at 15:22
  • Sorry, just corrected my typo. This method is the only one that is supposed to return `string`. All the others need to return `JSON`. So, I need to override this setting only for this particular method. – Sam Dec 28 '17 at 15:23
  • Add `[Produces(".....")]` to the action. – DavidG Dec 28 '17 at 15:25
  • Thank you! I needed to use `[Produces("text/plain")]`. Please post your comment as an answer so that I can accept it. Thanks again! – Sam Dec 28 '17 at 15:31
  • Just remove ```[Produces("application/json")]```, see: https://stackoverflow.com/a/73806345/9258504 – SamBerk Sep 21 '22 at 20:06

2 Answers2

7

As discussed in the comments you have a [Produces] attribute on the class that is forcing a JSON response. From the docs on ProducesAttribute we can see it can be applied to an action as well as the controller. So, you can override for a particular action by adding it there, in your case you need text/plain:

[Produces("text/plain")]
public async Task<IActionResult> GetSAS(string blobUri, string _method)
{
    //snip
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
4

You're returning an OkResult, which will serialize the object passed into it. Since that object is a string, and since the default serializer is JSON, your response ends up as a JSON string, i.e. with double quotes around it.

To fix this, you can take a number of different paths. First, you can simply change the return type to string. That will cause a ContentResult to be wrapped around it, which will not result in serialization. For example:

public async Task<string> GetSAS(string blobUri, string _method)
{
    ...

    return sas;
}

Second, you can use the Produces attribute to dictate the return content type as text/plain.

[Produces("text/plain")]
public async Task<IActionResult> GetSAS(string blobUri, string _method)

Lastly, you can simply leave it as-is, and instead interpret the result correctly as JSON on the client-side. You can use JSON.parse in your JavaScript code to get the string value:

var sas = JSON.parse(result);
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Great explanation. Thank you! Because @DavidG spent time helping me, I picked his as the answer but at least wanted to thank you for the great explanation! – Sam Dec 28 '17 at 15:45
  • 1
    Sure. No problem. I figured as much. I just wanted to add the explanation and some alternative ways to solve the issue for posterity. – Chris Pratt Dec 28 '17 at 15:46
  • @Sam It's not about time, it's about the right answer - this one is clearly more detailed, I'd be happy if you switched it. – DavidG Dec 28 '17 at 15:48
  • I think I'll let the great people of SO and history decide :-) Thank you both very much! – Sam Dec 28 '17 at 15:50