question

ahmedsalah-1628 avatar image
0 Votes"
ahmedsalah-1628 asked RenaNi-MSFT answered

How to return bool value as true or false from web api ?

I work on web api asp.net core 3.1

I face issue I can't return true or false from web API compare excel as below
so if excel is identical
then return true
else
return false

 [HttpGet]
         [Route("CompareExcel")]
         public IActionResult CompareExcel()
         {
              
             var DisplayFileName = Request.Form.Files[0];
             string fileName = DisplayFileName.FileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
             string Month = DateTime.Now.Month.ToString();
             string DirectoryCreate = Path.Combine(myValue1, Month);// myValue1 + "\\" + Month + "\\" + fileName;
             CExcel ex = new CExcel();
            
             string error = "";
             int rowCount = 0;
          
             var filedata = ContentDispositionHeaderValue.Parse(Request.Form.Files[0].ContentDisposition).FileName.Trim('"');
             var dbPath = Path.Combine(DirectoryCreate, fileName);
             var InputfilePath = System.IO.Path.Combine(GetFilesDownload, "Gen.xlsx");
             using (var stream = new FileStream(dbPath, FileMode.Create))
             {
                 Request.Form.Files[0].CopyTo(stream);
                 stream.Flush();
                 stream.Close();
             }
             GC.Collect();
             bool areIdentical = ex.CompareExcel(dbPath, InputfilePath, out rowCount, out error);
             if (areIdentical == true)
             {
 // return true
             }
             else
             {
 // retur false
    
             }
         }
dotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RenaNi-MSFT avatar image
0 Votes"
RenaNi-MSFT answered

Hi @ahmedsalah-1628 ,

I face issue I can't return true or false from web API compare excel as below

That is because the return value type is IActionResult. More explanation you could check microsoft official document.

You could choose one of the following way to return bool value:

 return Ok(true);

Or:

 return new JsonResult(true);

If you allow to change the return type, you could also change like below:

 public ActionResult<bool> CompareExcel()
 {   
         return true;
 }

Or:

  public bool CompareExcel()
  {   
         return true;
  }


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,

Rena

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.