5

I've followed instructions on how creating a ServiceStack here at:

https://github.com/ServiceStack/ServiceStack/wiki/Create-your-first-webservice

I'm sure I have followed it to the letter, but as soon as I run the web application. I get a 'Snapshot' view of my response. I understand this happens when I don't have a default view/webpage. I set up the project as a ASP.net website, not a ASP.net MVC website. Could that be the problem?

Snapshot

I also wrote a test console application with the following C# code. It got the response as a HTML webpage rather than as a plain string e.g. "Hello, John".

static void sendHello()
        {
            string contents = "john";
            string url = "http://localhost:51450/hello/";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentLength = contents.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            // SEND TO WEBSERVICE
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(contents);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            string result = string.Empty;

            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }

            Console.WriteLine(result);
        }

How can I switch off the 'snapshot' view? What am I doing wrong?

4

3 回答 3

8

浏览器正在请求 html,因此 ServiceStack 正在返回 html 快照。

有几种方法可以停止快照视图:

  • 首先是使用servicestack提供的ServiceClient类。这些还具有执行自动路由和强类型化响应 DTO 的优势。
  • 下一种方法是将Accept请求的标头设置为类似application/jsonapplication/xml将响应分别序列化为 json 或 xml 的内容。这就是 ServiceClient 在内部所做的
    HttpWebRequest 请求 = (HttpWebRequest)WebRequest.Create(url);
    request.Accept = "应用程序/json";
    ...
  • 另一种方法是添加一个名为的查询字符串参数format并将其设置为jsonxml
    字符串 url = "http://localhost:51450/hello/?format=json";
于 2013-09-06T13:23:43.683 回答
1

提出特定格式请求是执行此操作的实用方法

string url = "http://localhost:51450/hello/?format=json";
于 2017-09-06T23:29:30.827 回答
0

我建议简单地删除此功能。

public override void Configure(Container container)
{
    //...
    this.Plugins.RemoveAll(p => p is ServiceStack.Formats.HtmlFormat);
    //...
}

现在所有带有Content-Type=text/html的请求都将被忽略。

于 2018-03-26T14:02:15.453 回答