Dealing with Special Characters # and & in Angular's http.get() Request URL
Take a look at my code first.
Angular Service
let versionsearch = "&";
let strweeksearch = "%23";
this.http.get(this.apiUrl + 'GetVersionInfo?vehicleVersion=' + versionsearch + '&structureWeek=' + strweeksearch,
{ withCredentials: true })
.map(response => <CoCApiResponse[]>response.json())
.catch(error => {
return Observable.throw(error);
});
Web API
[HttpGet]
public CoCApiResponse GetVersionInfo([FromQuery]string vehicleVersion, [FromQuery]string structureWeek)
{
//code
}
Explanation and Questions:
I have a GET method in the Web API with two string parameters. I am calling that REST service from the Angular http service via a query string which passes those parameters. However, if I pass values like "#" or "&" in the query string, I encounter the following problems.
Using "#":
The API URL breaks when passing "#" in the query string.
But I found a solution from this discussion: Passing a pound sign as a value in the query string of a URL using Angular
So I replaced "#" with "%23" in the Angular service like
strweeksearch.replace(/#/g, '%23');
.
Using "&":
The API URL treats it as three parameters, like
apiurl?versionsearch=&&strweeksearch=123455
As a result, the Web API method parameter "vehicleVersion" always receives NULL.
How can I handle this situation without having to replace every special character with
reserved characters
? I want all special characters to be accepted in thehttp.get()
. How do I address this in Angular or the Web API?