0

I work on angular 7 I face issue I can't call web API return Boolean from angular 7

so how call web API on angular 7 return true or false

   [HttpGet]
    [Route("CompareExcel/SelectedOptions")]
    public IActionResult CompareExcel( int SelectedOptions)
    {
        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 selectedFile = "";
        var filedata = ContentDispositionHeaderValue.Parse(Request.Form.Files[0].ContentDisposition).FileName.Trim('"');
        var dbPath = Path.Combine(DirectoryCreate, fileName);
        if (SelectedOptions == 1)
        {
            selectedFile = "Gen.xlsx";
        }
        else if(SelectedOptions == 2)
        {
            selectedFile = "DeliveryGeneration_Input.xlsx";
        }
        var InputfilePath = System.IO.Path.Combine(GetFilesDownload, selectedFile);
        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 Ok(true);
        
        }
        else
        {
            return Ok(false);
            
        }
    
    }

link used to call web API from angular

http://localhost:61265/api/DeliverySys/CompareExcel/SelectedOptions?=1

when call function above it return only Boolean true or false

if compare excel identical then return true

if compare excel Not identical then return false

so on angular service.ts
//what I write 
component Type script ts
what I write
html component
what I write 

updated post

on service ts i do as below :
CompareExcel(SelectedOptions)
{
  this.http.get('http://localhost:61265/api/DeliverySys/CompareExcel/SelectedOptions?=' + SelectedOptions).subscribe(result => {
    console.log(result);
});
}
on component ts
i call it as 

  this._dataService.CompareExcel(this.selectedoptions.toString())
so are this correct 

on component html 
what i write 

2 Answers 2

2

You need to use HttpClientModule of Angular.

To use it, you have to import HttpClientModule into your root module.

app.module.ts or core.module.ts

@NgModule({
  imports: [..., HttpClientModule]
})
export class AppModule {} 

Secondly, you can create a service to handle the logic behind the request.

foo.service.ts

@Injectable({
  providedIn: 'root'
})
export class FooService {
  constructor(private http: HttpClient) {}

  get(): Observable<boolean> {
    return this.http.get<boolean>(<url>);
  }
}

As you can see in the code, methods of HttpClient are generic, so by using it, we can manage that the API response has a boolean type.

Finally, you can use your service in component like the following;

constructor(private service: FooService) {}

  get(): void {
    this.service.get().subscribe(resp => {
      if (resp) {
        console.log('yayyy');
      } else {
        console.log('nooo');
      }
    });
  }

I have created a stackblitz example for you.

1

You can call web routes using the @angular/core module Http.

Import like so:

import { HttpClient } from '@angular/common/http';

Use like this:

constructor(
    private http: HttpClient
) { }

test () {
    // with observable
    this.http.get('url').subscribe(result => {
        console.log(result);
    });

    // with promise
    this.http.get('url').toPromise().then(result => {
        console.log(result);
    });
}

Do not forget to add this in your app.module.ts:

import { HttpClientModule } from '@angular/common/http';

// add it in your imports
@NgModule ({
    declarations: [],
    imports: [
        HttpClientModule
    ]
})

in your case you would want to do the following

Component.ts

import { YourService } from 'path/to/service';

...

constructor(
    private yourService: YourService,
) { }

...

async ngOnInit() {
    const result = await this.yourService.callMyFunction();
    console.log(result);
}

yourService.ts

async callMyFunction() {
    const result = await this.http.get(yourUrl);
    return result;
}

component.html

<p>{{result}}</p>
7
  • so on service how it write it please Commented Jun 17, 2021 at 8:56
  • use the test() function I have provided, replace the 'url' with your url, try to call the function from a component and figure it out by trying :)
    – mike
    Commented Jun 17, 2021 at 8:57
  • i divide my app to service and component ts and component html so what i write on service and what i write on component ts and what i write on html component Commented Jun 17, 2021 at 8:57
  • Ok I see, on component.ts goes all your logic, on service.ts goes all the reusable parts, on component.html goes your displays.
    – mike
    Commented Jun 17, 2021 at 8:58
  • @ahmedbarbary if the reply solved your issue, please mark it as resolved!
    – mike
    Commented Jun 17, 2021 at 10:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.