【问题标题】:How to pass string with special characters to Server side function如何将带有特殊字符的字符串传递给服务器端函数
【发布时间】:2019-01-14 17:26:04
【问题描述】:

我有一个创建标题的基本应用服务。当我的客户端页面使用特殊字符将参数值传递给我的服务器端函数时,会出现问号。

请问我该如何解决这个问题?

我当前的代码。

index.js

var title = "Búsq"
titleService.CreateTitle(title).success(function (data) {
    vm.title= data;
});

TitleAppService.cs

public string CreateTitle(string title)
{
    // title is received here as B�sq <- how do I resolve this, it should be Búsq
}

【问题讨论】:

  • title 参数是否有可能在入口路径中传递(作为query string 参数)?因为如果是这样 - 它应该首先被编码
  • @ymz 任何关于编码的提示将不胜感激
  • 你是如何将 index.js 存储在磁盘上的?尝试使用 utf-8 编码存储 index.js

标签: javascript c# angularjs


【解决方案1】:

假设title 参数作为url 的一部分传递,它应该在在发送到服务器之前 编码。在某些平台上,值应该在服务器端解码

客户端编码 - JS(来自MDN

titleService.CreateTitle(encodeURIComponent(title)).success(function (data) {
    vm.title= data;
});

服务器端解码 - C#(关注此answer

public string CreateTitle(string title)
{
    title = Server.UrlDecode(title);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多