1

我有一个 servicestack 服务器,它使用响应过滤器向我们的服务添加数据验证消息。我的 Put/Post 处理程序返回一个 HttpResult 对象,其中 Response 设置为我们的验证对象。然后,响应过滤器将此对象分解为原始 html 字符串,并将 Response 对象设置为该字符串。为了让它工作,我必须在“视图”中创建一个 String.md 文件以及一个空白的 html 模板文件。我现在看到的问题是,当我们的自定义 html 响应包含逗号“”时,jsv fromater 尝试序列化字符串并引发异常:

   at System.String.get_Chars(Int32 index)
   at ServiceStack.Text.Jsv.JsvTypeSerializer.EatMapKey(String value, Int32& i)
   at ServiceStack.Text.Common.DeserializeDictionary`1.ParseStringDictionary(String value)
   at ServiceStack.Text.Jsv.JsvReader`1.Parse(String value)
   at ServiceStack.Text.TypeSerializer.DeserializeFromString[T](String value)
   at ServiceStack.Html.ViewDataDictionary.PopulateModelState()
   at ServiceStack.Html.HtmlHelper.Init(IViewEngine viewEngine, ViewDataDictionary viewData)
   at ServiceStack.Html.HtmlHelper.Init(MarkdownPage markdownPage, Dictionary`2 scopeArgs, Boolean renderHtml, ViewDataDictionary viewData)
   at ServiceStack.Markdown.MarkdownViewBase.Init(IAppHost appHost, MarkdownPage markdownPage, Dictionary`2 scopeArgs, Object model, Boolean renderHtml)
   at ServiceStack.WebHost.Endpoints.Support.Markdown.MarkdownPage.Write(TextWriter textWriter, PageContext pageContext)
   at ServiceStack.WebHost.Endpoints.Support.Markdown.TemplateExtensions.RenderToString(MarkdownPage markdownPage, Dictionary`2 scopeArgs, Boolean renderHtml)
   at ServiceStack.WebHost.Endpoints.Formats.MarkdownFormat.RenderDynamicPage(MarkdownPage markdownPage, Dictionary`2 scopeArgs, Boolean renderHtml, Boolean renderTemplate)
   at ServiceStack.WebHost.Endpoints.Formats.MarkdownFormat.RenderDynamicPage(MarkdownPage markdownPage, String pageName, Object model, Boolean renderHtml, Boolean renderTemplate)
   at ServiceStack.WebHost.Endpoints.Formats.MarkdownFormat.ProcessMarkdownPage(IHttpRequest httpReq, MarkdownPage markdownPage, Object dto, IHttpResponse httpRes)
   at ServiceStack.WebHost.Endpoints.Formats.MarkdownFormat.<Register>b__1(IRequestContext requestContext, Object dto, IHttpResponse httpRes)
   at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.<>c__DisplayClass1.<SerializeToStream>b__0(StreamSerializerResolverDelegate x)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.SerializeToStream(IRequestContext requestContext, Object dto, IHttpResponse httpRes)
   at ServiceStack.Common.Web.HttpResponseFilter.SerializeToStream(IRequestContext requestContext, Object response, Stream responseStream) in i:\GitHub\ServiceStack\src\ServiceStack.Common\Web\HttpResponseFilter.cs:line 152
   at ServiceStack.Common.Web.HttpResult.WriteTo(Stream responseStream) in i:\GitHub\ServiceStack\src\ServiceStack.Common\Web\HttpResult.cs:line 219
   at ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToOutputStream(IHttpResponse response, Object result, Byte[] bodyPrefix, Byte[] bodySuffix)
   at ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponse(IHttpResponse response, Object result, ResponseSerializerDelegate defaultAction, IRequestContext serializerCtx, Byte[] bodyPrefix, Byte[] bodySuffix)

我的问题是如何为这些响应关闭 jsv?还是有更好的方法来返回原始 html?我不能让我的请求处理程序返回一个字符串,因为我需要控制返回的响应代码和标头。

4

1 回答 1

0

您可以在 HttpResult 中返回 HTML 字符串,例如:

return new HttpResult("<html>...</html>", ContentType.Html);

参考 ServiceStack 的 HTML 故事可以在以下位置找到:

是否可以使用 ServiceStack 提供 HTML 页面?

除了 Markdown,我们还添加了新的 MVC Razor 支持,您可以在以下位置查看示例:

于 2012-08-14T00:09:10.097 回答