2009-02-04 28 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); 
    } 
}