2009-02-04 60 views
5

所以這裏有一個非常簡單的界面,用於在WCF中休息。WCF REST參數不能包含句點?

[ServiceContract] 
    public interface IRestTest 
    { 
     [OperationContract] 
     [WebGet(UriTemplate="Operation/{value}")] 
     System.IO.Stream Operation(string value); 
    } 

它的偉大工程,直到我試圖通過與這時期,如DNS名稱的字符串...我收到了404了asp.net的。

更改UriTemplate以將參數粘貼到查詢字符串中會導致問題消失。任何人看到這個或有一個解決方法?

+0

+1爲查詢字符串建議。期限在請求的任何地方都可以正常工作,除了最後。 – 2010-07-04 21:28:32

+1

將參數粘貼到查詢字符串時,UriTemplate(和URL)的外觀如何? – dumbledad 2013-04-29 08:06:05

回答

1

我幾乎確切的簽名服務。我可以傳遞具有「。」的值。在名字裏。與URL http://localhost/MyService.svc/category/test.category我得到傳遞進來的字符串值取值爲「test.category」

[OperationContract] 
[WebGet(UriTemplate = "category/{id}")] 
string category(string id); 

:例如,這會在我的工作。

所以一定有其他問題。你如何訪問網址?只是直接在瀏覽器中?或通過JavaScript調用?只是想知道這是否是客戶端的一些錯誤。服務器傳遞的值很好。我會建議嘗試訪問您的瀏覽器中的網址,如果它不起作用,那麼請準確地發佈您正在使用的URL以及錯誤消息的內容。

此外,你使用WCF 3.5 SP1或只是WCF 3.5?在我閱讀的RESTFul .Net書中,我看到UriTemplate有一些變化。

最後,我從RESTFul .Net書中修改了一個簡單的Service,並且能夠正確迴應。

class Program 
{ 
    static void Main(string[] args) 
    { 
     var binding = new WebHttpBinding(); 
     var sh = new WebServiceHost(typeof(TestService)); 
     sh.AddServiceEndpoint(typeof(TestService), 
      binding, 
      "http://localhost:8889/TestHttp"); 
     sh.Open(); 
     Console.WriteLine("Simple HTTP Service Listening"); 
     Console.WriteLine("Press enter to stop service"); 
     Console.ReadLine(); 
    } 
} 

[ServiceContract] 
public class TestService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "category/{id}")] 
    public string category(string id) 
    { 
     return "got '" + id + "'"; 
    } 
} 
+0

對於踢腿,我試過你的接口,以防它與流類型返回相關,並且我仍然從asp.net獲得404的http://localhost/Software.svc/category/test.category 這是一個URL通過地址欄進入。如果談到它,我會製作一個適用於該bug的IHttpModule。 – 2009-02-06 20:06:56

1

下面是一個HttpModule示例,它修復了在「REST」參數中出現的'period'。請注意,我只在開發服務器(又名卡西尼)中看到過這種情況,在IIS7中它似乎沒有這種「破解」。我在下面包含的例子也取代了我從這個答案改編的文件擴展名'.svc'。 How to remove thie 「.svc」 extension in RESTful WCF service?

public class RestModule : IHttpModule 
{ 
    public void Dispose() 
    { 
    } 

    public void Init(HttpApplication context) 
    { 
     context.BeginRequest += 
      delegate 
      { 
       HttpContext ctx = HttpContext.Current; 
       string path = ctx.Request.AppRelativeCurrentExecutionFilePath; 

       int i = path.IndexOf('/', 2); 
       if (i > 0) 
       { 
        int j = path.IndexOf(".svc", 2); 
        if (j < 0) 
        { 
         RewritePath(ctx, path, i, ".svc"); 
        } 
        else 
        { 
         RewritePath(ctx, path, j + 4, ""); 
        } 
       } 
      }; 
    } 

    private void RewritePath(HttpContext ctx, string path, int index, string suffix) 
    { 
     string svc = path.Substring(0, index) + suffix; 
     string rest = path.Substring(index); 
     if (!rest.EndsWith(ctx.Request.PathInfo)) 
     { 
      rest += ctx.Request.PathInfo; 
     } 
     string qs = ctx.Request.QueryString.ToString(); 
     ctx.RewritePath(svc, rest, qs, false); 
    } 
}