【发布时间】:2021-06-13 09:49:31
【问题描述】:
我在将数据从 angular 传递到 webapi 时遇到问题。 我需要从我的数据库中翻译一些短语,一切正常,直到我的短语看起来像这样:
“无休息日”
因为在这种情况下,我对 webapi 的请求看起来像:
https://localhost:44973/api/translation/getResstring/day%20w/o%20break
还有那个字符 / 破坏请求。 如何正确地将其传递给 WebApi?我昨天匆忙做了,在 Angular 端编码并在 Web Api 端解码,但它不起作用,所以我决定恢复它。
昨天的尝试,角度应用:
[...]
public getResstringByPhrase(
source: string
): Observable<string> {
const result = this.http.get(this.url + "getResstring/" + source, { responseType: 'text' })
return result
}
[...]
.net Core 网络接口:
[HttpGet("{*phrase}")]
[Route("getResstring/{phrase}")]
public IActionResult Get(string phrase)
{
var resstring = _translationRepository.GetResstringByPhrase(phrase);
return new OkObjectResult(resstring);
}
Startup.cs(仅配置):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
} }
但即使有这种尝试,它也不适用于带有“/”符号的短语
#更新
冲突的操作:
[HttpGet("{languageCharset}/{resstring}")]
[Route("{languageCharset}/{resstring}")]
public IActionResult Get(string resstring, LanguageCharset languageCharset)
{
var translate = _translationRepository.GetTranslatedByResstring(resstring, languageCharset);
return new OkObjectResult(translate);
}
#更新 2:
我做到了,现在“/”可以工作,但是“+”有问题。代码
Webapi:
[HttpGet("{phrase}")]
[Route("getResstring/{phrase}")]
public IActionResult Get(string phrase)
{
phrase = HttpUtility.UrlDecode(phrase);
var resstring = _translationRepository.GetResstringByPhrase(phrase);
return new OkObjectResult(resstring);
}
Angular 应用:
if( translatedElements[index].getIsTranslated() === false ) {
this.getResstringByPhrase(encodeURIComponent(translatedElements[index].getValue())).subscribe(async res => {
const translatedLabel = await this.GetTranslatedByResstring(res, 1045).toPromise()
if (translatedLabel.getPhrase() !== '') {
translatedElements[index].setValue(translatedLabel.getPhrase())
}
})
}
现在错误是(仅当短语中有“+”时出现):
HTTP 错误 404.11 - 未找到
请求过滤模块被配置为拒绝替代双重解决方案的取消。 (对不起,我的语言翻译)
【问题讨论】:
标签: angular asp.net-core request asp.net-core-webapi