Obtaining parameters from a URL using C# and AngularJS

I'm encountering difficulties when trying to fetch parameters from my API URL. Any assistance would be highly appreciated.

This is the C# code snippet

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

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

       }

       [HttpGet]
       // The data isn't being returned...
       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's the TypeScript code for Angular

 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;
     }
   });
 }

Take a look at the screenshot from my browser console

https://i.sstatic.net/aKlv7.gif

Answer №1

To include a parameter in your method, you can use the following example:

 function fetchData(searchParam): void {
     this.http.get('api/data?param=' + searchParam).subscribe(results => {
         this.data = results.json();
 //additional code here 

Please be aware that I am not certain about the exact TypeScript syntax for parameters, so it may require adjustment.

Answer №2

If I understand correctly, you are attempting to access the api/password endpoint by passing the get parameter ?emp=Test from an Angular web application.

It seems like instead of calling the endpoint with the parameter, you are actually calling the entire Angular app with the parameter. My recommendation would be to make the following call in your typescript code:

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

This should help resolve the issue you're facing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Waiting specifically for an element to appear using Selenium Webdriver

Currently, I am utilizing the Selenium WebDriver with C# and implementing the page object module. At this point, I require a syntax for an explicit wait where I already have the webelement available. [FindsBy(How = How.Id, Using = "Passwd")] public IWebEl ...

Angular template error: Potential is present but not defined

Encountering an issue with my Angular application's template file (dashboard.component.html). The error message reads "Object is possibly 'undefined'." Here's the portion of the template that seems to be causing the problem: <div> ...

Calculating the angle between two planes in Three.js is a useful technique

I'm currently developing a project with the three.js library, and I am curious about how to calculate the angle (let's refer to it as α) between two plane geometries. You can find a helpful visual representation of the planes in this image. ...

What is the best way to extract user input from a bootstrap search bar and integrate it into an ajax request to fetch information?

I am currently utilizing HTML, Javascript, and bootstrap to develop a web application. However, I have encountered an obstacle. When using the code document.getElementById("input here"), it only returned an array of 0. My goal is to retrieve data from an A ...

The button mysteriously vanishes on Github Pages, yet remains visible when viewing the

I recently completed a project on Github and created a corresponding Github Page for it. The page features multiple buttons with different functions, but oddly enough, one specific button is not showing up on the Github Page. Surprisingly, it appears perfe ...

Ways to divide the vuetify-text-field-label into two lines

My vuetify text field has a label that is too long for mobile devices. I am wondering if there is a way to automatically wrap the text. I attempted using div and p, as shown in this codepen link <div id="app"> <v-app id="inspire ...

Preserving arrays and objects inside the specified array

I am currently working on developing a simple to-do app with vue.js. My goal is to save the to-dos stored in an array so that they persist even after resetting the site. After browsing through some documentation, I came across the following solution: data( ...

Developing a universal SDK wrapper for Firebase services (Firestore, Cloud Storage, and additional functionalities)

Currently, I am on the quest to discover an abstraction that can facilitate the seamless operation of Firebase products (specifically Firestore, Storage, and Analytics) across any platform (be it React Native, React, or Node.js). While considering the REST ...

Exploring the blur() function in JavaScript through cold calling

There is a specific line of code that I need help with. document.getElementById("firstName").addEventListener("blur", validateField); Additionally, there is this block of code: validateField = (event) => { const el = event.targe ...

What is the best way to generate a circle around a specific point on the map without using a predetermined radius centered on a marker?

If I want to modify the code to respond to a click instead of creating a circle around a predefined marker, what changes can I make? Here is the code sample: https://developers.google.com/maps/documentation/javascript/examples/circle-simple <scri ...

How to obtain the index of a row in a ComboBox through the Devexpress DataBinding method?

<dx:GridViewDataComboBoxColumn Caption="<%$Resources: zione%>" Width="15%" Visible="true"> <DataItemTemplate> <dx:ASPxComboBox ID="comboZione" runat="server" ...

Is there a way to trigger a jQuery function using the onRowSelect event in ajax?

I have a jQuery function that is triggered by the 'change' event when a value is selected in a <select> element. However, I no longer need the <select> because I will manually set the value within my jQuery function. My page utilizes ...

Creating a unique 32-bit unsigned integer identifier for use in Couchbase

I am in search of a method to create a distinct ID for a nosql database. Unlike traditional relational databases, there are no rows to increment from. Typically, UUID's are used to address this issue. However, I require an additional ID (apart from t ...

Combining Vue.js and OMDb pages using routing capabilities

I am currently working on implementing the bootstrap-vue pagination nav feature in my project. The goal is to update an ajax call with the requested page number when a new page button is clicked. Here is a snippet of my router: export default new Router( ...

Results produced by an error category in the App_Code directory

I am currently investigating exceptions occurring on the development client's dev server (our servers are running smoothly). To handle most of these exceptions, I have a TestException.cs class that is specifically designed for dealing with exceptions ...

Optimizing Multiple AJAX Requests

Currently, I am in the process of developing an ASP.NET application that utilizes WCF services for AJAX requests. The functionality is working as expected; however, there seems to be a significant delay in loading times. After inspecting the network tab in ...

Access Oracle database on .net without the need for client installation

I am looking to establish a connection with an oracle database without the need for installation. In Java, I can use a straightforward jdbc library. However, I am curious about the simplest approach in .NET C# to connect to an oracle database. Additionally ...

What is the purpose of encoding the Jquery Ajax URL twice?

In my JavaScript code, I am utilizing the jQuery ajax function. Here is an example of how it appears: $.ajax({ url: "somepage/" + keyword + "/" + xyz + "/" + abc + "/getbla", (...) If the 'keyword' (value from a textfield) includes a ' ...

The file or assembly "Dotspatial" failed to load

In the process of creating a user control that utilizes DotSpatial 1.7, I encountered an issue while testing it in a windows form app. Initially, when setting "Copy Local" to true for the referenced "DotSpatial" dll files, the control worked perfectly. How ...

Retrieve the HTML cell value in ASP.Net code behind by clicking操作

Not being an expert in C# and ASP.Net, I appreciate your patience with me as I try to tackle a problem. The issue at hand is that I am utilizing the ASP.Net WebForm API with C# to create a dashboard. I have a generic HTML table populated from a SQL query ...