WCF REST parameters cannot contain periods?

So, here is a super simple rest interface in WCF.

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

It works fine until I try to pass a string with periods in it, like a DNS name ... I get 404 from asp.net.

Changing the UriTemplate to insert parameters into the query string makes the problem go away. Does anyone else see this or have a workaround?

+5
source share
3 answers

It is true that part of the path cannot contain a period or many other special characters. I experienced the same problem a while ago and received a response from the TechNet team stating that the request is your only option. Unfortunately

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/d03c8331-1e98-4d5d-82a7-390942a93012/

+3

. , ".". . , :

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

url http://localhost/MyService.svc/category/test.category ` "test.category" .

, - . URL-? ? javascript-? , . . URL- , , , URL- , .

, WCF 3.5 SP1 WCF 3.5? RESTFul.Net , , UriTemplate.

, , RESTFul.Net, , .

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 + "'";
    }   
}
+1

Here is an example HttpModule that captures the "period" when they occur in REST parameters. Please note that I only saw this on the development server (aka Cassini), in IIS7 it works without this “hacking”. The example below also replaces the .svc file extension that I adapted from this answer. How to remove extension ".svc" 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);
    }
}
+1
source

All Articles