[英]How can I use a query string with a WCF Service?
我正在嘗試創建 Web 服務,到目前為止它們一直工作得很好,但我有一個問題:如果我的查詢字符串不是我指定的確切順序,代碼會給出不正確的結果。
在開始一個大型項目之前,我希望能夠傳入一個查詢字符串,以便順序無關緊要 - 傳入“?user=foo&pass=bar”應該等同於“?pass=bar&user=foo”,但我只是不確定如何按預期工作。
事實上,我不會因為更改查詢字符串參數而出錯,而是 DBAgent.authenticate() 只會按順序接受參數,而不管查詢字符串中的參數名稱如何。
我錯過了什么?
IDBAgent.cs:
public interface IDBAgent
{
[OperationContract]
[WebInvoke(UriTemplate = "/authenticate/?username={username}&password={password}", Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
string authenticate(string username, string password);
}
DBAgent.svc.cs
public class DBAgent : IDBAgent
{
public string authenticate(string username, string password)
{
return runSQL("EXEC sp_Authenticate '" + username + "', '" + password + '\'');
}
}
索引.html:
var output = "";
function callService(url, params)
{
try {
return $.ajax({
type: "GET",
async: false,
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
processdata: true,
success: function (msg) {
output = msg;
},
error: function(){console.log("Oops.")}
});
}
catch (e) {
console.log("Something went wrong! (╯°□°)╯︵ ┻━┻");
}
}
function authenticate(user, pass)
{
callService("http://localhost/DBAgent.svc/authenticate/?username=foo&password=bar", []).done(); // Returns true
console.log(output);
callService("http://localhost/DBAgent.svc/authenticate/?password=bar&username=foo", []).done(); // Returns false
console.log(output);
callService("http://localhost/DBAgent.svc/authenticate/?password=foo&username=bar", []).done(); // Returns true
console.log(output);
}
對於這種情況,您可能應該考慮使用“POST”而不是“GET”。 另外,請注意,在使用 UriTemplate 時,查詢字符串參數的順序無關緊要。 ?user=foo&pass=bar" 在結構上等同於 "?pass=bar&user=foo",
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.