我有一个RESTful WCF服务,我正在执行一个使用jQuery.ajax()的PUT服务,我的一个操作以5个字符串作为参数。此操作对RDF文档执行任务,因此其中一些参数在字符串中包括(#)。为了适应这种情况,我对这些参数进行编码。
我的问题是,当url包含这些编码的参数时,请求在404中失败。出于测试目的,我省略了#,并且请求执行正常。我不明白为什么编码的# (%23)会导致404。有人能帮我弄明白怎么回事吗?
行动:
[OperationContract]
[CorsBehavior]
[WebInvoke(Method = "PUT", UriTemplate = "Graphs/{library}/{subjectLocalPart}/{predicateLocalPart}/{objectPart}/{languageCode}")]
ResultMessage CreateTriple(string library, string subjectLocalPart, string predicateLocalPart, string objectPart, string languageCode);404:
http://localhost:1605/Service.svc/Graphs/myLib/123abc/content%23HasA/456def%23ghik/en-us
作品:
http://localhost:1605/Service.svc/Graphs/myLib/123abc/contentHasA/456defghik/en-us
发布于 2012-01-08 20:53:11
我将把我的意见作为答复的形式。
即使在您看来它是URL编码的,但翻译的URL如下所示:
http://localhost:1605/Service.svc/Graphs/myLib/123abc/content#HasA/456def#ghik/en-us因此,您的“页面”实际上是123abc/content,这就是为什么它是404。
要解决这个问题,请在WCF服务方法中使用参数和DTO (域对象或类)作为参数。它可以是JSON或name=value对;就我个人而言,我使用JSON。
[OperationContract]
[CorsBehavior]
[WebInvoke(Method = "PUT", UriTemplate = "Graphs/{library}/triple")]
ResultMessage CreateTriple(string library, TripleModel model);
[DataContract]
public class TripleModel {
[DataMember]
public string SubjectLocalPart { get; set; }
[DataMember]
public string PredicateLocalPart { get; set; }
[DataMember]
public string ObjectPart { get; set; }
[DataMember]
public string LanguageCode { get; set; }
}我还没有100%测试这一点,但再次检查使用Fiddler只是为了确保这种情况。我猜是根据你以前的经验和你给我们的。
发布于 2012-01-08 23:25:15
正如另一个答案所指出的,下面的两个URL本质上是相同的。
http://localhost:1605/Service.svc/Graphs/myLib/123abc/content%23HasA/456def%23ghik/en-us
http://localhost:1605/Service.svc/Graphs/myLib/123abc/content#HasA/456def#ghik/en-us如果要在URI中发送“#”,则需要转义已转义的版本,以便当它在服务器上未转义时,它将达到您的预期:
http://localhost:1605/Service.svc/Graphs/myLib/123abc/content%2523HasA/456def%2523ghik/en-ushttps://stackoverflow.com/questions/8765341
复制相似问题