How to pass multiple parameters from angular 8 to .NET Core API

Matija

Hello i'm not sure if anyone posted this question but since it's my first one I will try and be as precise as i can.

I'm trying to pass multiple string params to a .NET Core API from an Angular 8 app and to no avail.

This is the code in angular:

import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';

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

  searchSpecificID(formattedNumber: string, vin: string): Observable<number> {

    const paramsIoS = new HttpParams().set('formattedNumber', formattedNumber).set('vin', vin);

    return this.http.get<number>('https://localhost:44353/api/AngularTest/CheckIfIDExists',  {params: paramsIoS}).pipe(
      tap(data => console.log('Checked'),
      catchError(this.handleError))
    );
  }

  private handleError(err: HttpErrorResponse) {
    let errorMessage = '';
    if (err.error instanceof ErrorEvent) {
      errorMessage = `An error occured: ${err.error.message}`;
    } else {
      errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
    }
    console.error(errorMessage);
    return throwError(errorMessage);
  }
}

And this is the code in the .NET Core API Controller:

 [Route("api/[controller]")]
 [ApiController]
 public class AngularTestController : ControllerBase
 {
    [HttpGet("CheckIfIDExists/{formattedNumber}/{vin}")]
    public int CheckIfIDExists(string formattedNumber, string vin) 
    {
        return 0;
    }
 }

I've tried some combinations from these questions (.NET Core API pass multiple, Angular HTTP get multiple parameters) but had no success so far.

I've enabled cors in my .NET Core API application because I have some HTTP Get requests without parameters and they work fine.

Farhat Zaman

just try like this

let headers = new HttpHeaders();
headers  = headers.append('responseType', 'json');
return this.http.get<number>(`http://localhost:44353/api/AngularTest/${formattedNumber}/${vin}`, {headers: headers});

and you backend code should be like

[Route("api/[controller]")]
 [ApiController]
 public class AngularTestController : ControllerBase
 {
    [HttpGet("{formattedNumber}/{vin}")]
    public int CheckIfIDExists(int formattedNumber, string vin) 
    {
        return 0;
    }
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From

How should I pass multiple parameters to an ASP.Net Web API GET?

From Dev

How to pass parameters to a .NET core project with dockerfile

From Dev

How to pass data and image both to "ASP.NET Core" Web API using Angular 2(typescript)?

From Dev

Pass multiple parameters in a POST API without using a DTO class in .Net Core MVC

From Dev

Asp.Net core ActionFilter - how to pass varying number of parameters

From Dev

Accept multiple parameters for API in .NET Core 2.0 using attribute routing

From Dev

.NET Core + Angular-CLI App - can't pass data from api to components

From Dev

Accept multiple parameters for API in .NET Core 2.0

From Dev

How do I pass array from Angular 6 to ASP.NET Core API for GET method?

From Dev

How to pass multiple checkbox parameters from foreach

From Dev

How to pass multiple parameters to Get Method and Route them in .NET Core Web API?

From Dev

How to send an array from angular 8 to web api core?

From Dev

.net core web api - Can controller pass parameters to middleware?

From Dev

How to send image file from Angular to .net core API

From Dev

How to pass multiple parameters to stored procedure on .net core 3.1

From Dev

how to pass multiple parameters to the web API?

From Dev

JasperReports API: How to pass multiple parameters to report

From Dev

How to pass multiple parameters from Angular form to make POST request?

From Dev

How to receive multiple object parameters in GET Action in ASP.NET Core 2.2 REST API?

From Dev

How to I pass multiple parameters from a form to a controller in Asp.net MVC core?

From Dev

How to pass parameters to Ajax to asp.Net core

From Dev

Http headers passed from Angular 8 are not appearing in HttpContext in ASP.NET Core API 3.1

From Dev

Pass data from Angular to .Net Core Web Api with "/" character

From Dev

How to pass two parameters to a controller in ASP.NET Core MVC?

From Dev

How to call API with multiple parameters from .NET Core MVC

From Dev

ASP.NET Core - API multiple parameters

From Dev

How to pass three parameters to url address using ASP.NET Core Web API

From Dev

How to pass multiple parameters from .Net console app to Class Library?

From Dev

How to pass parameters to an API url from JS

Related Related

  1. 1

    How should I pass multiple parameters to an ASP.Net Web API GET?

  2. 2

    How to pass parameters to a .NET core project with dockerfile

  3. 3

    How to pass data and image both to "ASP.NET Core" Web API using Angular 2(typescript)?

  4. 4

    Pass multiple parameters in a POST API without using a DTO class in .Net Core MVC

  5. 5

    Asp.Net core ActionFilter - how to pass varying number of parameters

  6. 6

    Accept multiple parameters for API in .NET Core 2.0 using attribute routing

  7. 7

    .NET Core + Angular-CLI App - can't pass data from api to components

  8. 8

    Accept multiple parameters for API in .NET Core 2.0

  9. 9

    How do I pass array from Angular 6 to ASP.NET Core API for GET method?

  10. 10

    How to pass multiple checkbox parameters from foreach

  11. 11

    How to pass multiple parameters to Get Method and Route them in .NET Core Web API?

  12. 12

    How to send an array from angular 8 to web api core?

  13. 13

    .net core web api - Can controller pass parameters to middleware?

  14. 14

    How to send image file from Angular to .net core API

  15. 15

    How to pass multiple parameters to stored procedure on .net core 3.1

  16. 16

    how to pass multiple parameters to the web API?

  17. 17

    JasperReports API: How to pass multiple parameters to report

  18. 18

    How to pass multiple parameters from Angular form to make POST request?

  19. 19

    How to receive multiple object parameters in GET Action in ASP.NET Core 2.2 REST API?

  20. 20

    How to I pass multiple parameters from a form to a controller in Asp.net MVC core?

  21. 21

    How to pass parameters to Ajax to asp.Net core

  22. 22

    Http headers passed from Angular 8 are not appearing in HttpContext in ASP.NET Core API 3.1

  23. 23

    Pass data from Angular to .Net Core Web Api with "/" character

  24. 24

    How to pass two parameters to a controller in ASP.NET Core MVC?

  25. 25

    How to call API with multiple parameters from .NET Core MVC

  26. 26

    ASP.NET Core - API multiple parameters

  27. 27

    How to pass three parameters to url address using ASP.NET Core Web API

  28. 28

    How to pass multiple parameters from .Net console app to Class Library?

  29. 29

    How to pass parameters to an API url from JS

HotTag

Archive