滞后于 # 和 & 在 HTTP 请求的角度



#and&在Angular http.get()中请求URL

请首先查看我的代码。

Angular Service

let versionsearch ="&";
let strweeksearch="#";
this.http.get(this.apiUrl + 'GetVersionInfo?vehicleVersion=' + versionsearch + '&structureWeek=' + strweeksearch,
            { withCredentials: true })
            .map(responce => <CoCApiResponse[]>responce.json())
            .catch(error => {
                return Observable.throw(error);
            });

Web API

  [HttpGet]
  public CoCApiResponse GetVersionInfo([FromQuery]string vehicleVersion, [FromQuery]string structureWeek)
        {
            //code
        }

解释&amp;问题:

我在Web API中有一个GET方法,它具有两个字符串参数。我只是通过查询字符串从Angular http服务中调用REST服务,这些服务传递了这些参数。但是,尤其是如果要通过该值是#或查询字符串中的&,我会得到以下问题。

使用#

如果在查询字符串中通过#,API URL将断开。

,但我从这个讨论中对此有所了解:使用Angular

将一个磅标志作为查询字符串中的值传递给一个值

所以我这样做是为了将#替换为Angular服务的%23 strweeksearch.replace(/#/g, '%23');


使用&

API URL将其视为3个参数,例如

apiurl?versionsearch =&amp;&amp; strweeksearch = 123455

所以Web API方法参数vehicleVersion总是得到NULL


如何实现这一目标?我不想将每个特殊字符替换为reserved characters。我需要在http.get()中接受每个特殊字符。如何在 Angular 或Web API中处理此操作?

您在get()中添加版本搜索,并加上字符串加上'&amp;'同样,这是放心的想法,您正在传递3个参数。

this.http.get(this.apiUrl + 'GetVersionInfo?vehicleVersion=' + versionsearch + 'structureWeek=' + strweeksearch,
            { withCredentials: true })
            .map(responce => <CoCApiResponse[]>responce.json())
            .catch(error => {
                return Observable.throw(error);
            });

尝试删除一个!

使用Encodeuri或Encodeuricomponent。

最新更新