0

I am having issues retrieving parameters from my URL that is an API. Any help would be greatly appreciated.

Here is my c# code

[Route("api/[controller]")]
   public class PasswordController : Controller
   {
       private readonly AppSettings _options;

       public PasswordController(IOptions<AppSettings> optionsAccessor)
       {
           _options = optionsAccessor.Value;

       }

       [HttpGet]
       //this isnt returning anything...
       public IActionResult Get([FromQuery]string emp)
       {
           var x = HttpContext.Request.Query["emp"].ToString();
               Response.Headers.Add("x-emp-name", x);

           return Json(_options.ClientSettings);
       }

Here is my angular code ts

 private GetData(): void {
     this.http.get('api/password').subscribe(values => {
     this.ViewOptions = values.json();
     this.titleService.setTitle(this.ViewOptions.changePasswordTitle + " -
" + this.ViewOptions.applicationTitle);
     if (this.ViewOptions.recaptcha.isEnabled) {
       this.FormGroup.addControl('reCaptcha', new FormControl('',
[Validators.required]));
       const sp = document.createElement('script');
       sp.type = 'text/javascript';
       sp.async = true;
       sp.defer = true;
       sp.src =
'https://www.google.com/recaptcha/api.js?onload=vcRecaptchaApiLoaded&render=explicit&hl='
+ this.ViewOptions.recaptcha.languageCode;
     }
   });
 }

Here is my browser console screenshot

Screenshot

2
  • Your URL also doesn't look correct. It should (?) be something like api/password?emp= Commented Feb 22, 2018 at 19:26
  • Thanks for the comment how would I had a var param to the url in my angular code? @MarkC. Commented Feb 23, 2018 at 15:31

2 Answers 2

1

You can pass a parameter into your method:

 private GetData(queryParam): void {
     this.http.get('api/password?emp=' + queryParam).subscribe(values => {
     this.ViewOptions = values.json();
 ///code omitted 

note: I don't know the exact syntax for TypeScript parameters so this probably isn't copy/paste-able.

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

Comments

1

So as I understand your question, you try to call the api/password endpoint with the get parameter ?emp=Test from within an angular web app.

As I see you don't call the endpoint with the parameter but call the whole angular app with the parameter. So my suggestion was to call

this.http.get('api/password?emp=Test').subscribe

from the typescript code.

2 Comments

How would I call this with a variable because the param wouln't always be "Test"? Thanks for you answer btw @Corristo
did your controller receive "Test" in the emp param? If yes pls mark my answer as correct cause this was what the question was about. As Mark C. wrote with this.http.get('api/password?emp=' + param) you can build your request url with JS variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.