0

I have written web api in which it is returning/producing json response. Below is the code for the same.

[HttpGet]
[Route("someapi/myclassdata")]
[Produces("application/json")]
public MyClassData GetMyClassData(int ID, int isdownload)
{
   myclassdata = myclassdataBL.GetMyClassData(ID);
   return myclassdata;

    //**Commented Code**
   //if(isdownload==1)
   //{
        //download file
   //}
   //else
   //{
        // send response
   //}
}

Till now it is working fine. Now I want to create and download the file based on value in 'isDownload' parameter.

So after getting the data if files needs to be downloaded I want to download the file otherwise I will send the json response.

So, my question is can we send json reponse or download the file in same web api method.

Any help on this appreciated!

1
  • 1
    I would avoid using the same Web API method to return json in some cases or a file in other cases. Why don't you create 2 distinct Web API methods (one returns JSON and the other returns the file) and let the client application decide which one to invoke? Commented Sep 22, 2018 at 11:00

1 Answer 1

1

Yes, this is easily achievable. Rather than returning MyClassData explicitly like that, you can return an IActionResult, like this:

public IActionResult GetMyClassData(int ID, int isdownload)
{
   myclassdata = myclassdataBL.GetMyClassData(ID);

   if (isDownload == 1)
       return File(...);

   return Ok(myclassdata);
}

Both File(...) and Ok(...) return an IActionResult, which allows for the dynamism you're asking for here.

Note: You can also just return Json(myclassdata) if you want to force JSON, but by default this will be JSON anyway and is more flexible with e.g. content-negotiation.

See Controller action return types in ASP.NET Core Web API for more details.

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

6 Comments

So there is no need to have [Produces("application/json")] at top of the method ?
Personally, I've never needed to use [Produces(...)]. You don't need it here, but I believe it is also used by Swagger and such documentation-generation features. See here for more.
Yes, we are using swagger to test the APIs.. So, if I remove this still the json will be shown in swagger ?
Yes, assuming you're working with the defaults (this is likely), you will end up with JSON being returned. The benefit of using the [Produces] attribute generally is to inform Swagger that the response will indeed be JSON - it can also be used to document e.g. the status-codes that can be returned, as Swagger isn't always able to determine things like that for itself. You could also add something like [Produces("application/pdf")] if the file can return a PDF file, for example.
No problem at all. This might also be helpful. You can be very descriptive of what will be returned from your actions.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.