使用ServiceStack.Razor我得到IndexOutOfRangeException

时间:2013-04-08 11:35:00

标签: asp.net razor servicestack

我想让ServiceStack.Razor工作,但我得到的是IndexOutOfRangeException。堆栈跟踪包含在下面:

at ServiceStack.Text.Jsv.JsvTypeSerializer.EatMapKey(String value, Int32& i) 
at ServiceStack.Text.Common.DeserializeDictionary`1.ParseStringDictionary(String value)
at ServiceStack.Html.ViewDataDictionary.PopulateModelState() 
at ServiceStack.Razor.ViewPage`1.Init(IRazorViewEngine viewEngine, ViewDataDictionary viewData, IHttpRequest httpReq, IHttpResponse httpRes) 
at ServiceStack.Razor.Templating.TemplateService.InitTemplate[T](T model, ITemplate instance, IHttpRequest httpReq, IHttpResponse httpRes) 
at ServiceStack.Razor.Templating.TemplateService.ExecuteTemplate[T](T model, String name, String defaultTemplatePath, IHttpRequest httpReq, IHttpResponse httpRes) 
at ServiceStack.Razor.RazorFormat.ProcessRazorPage(IHttpRequest httpReq, ViewPageRef razorPage, Object dto, IHttpResponse httpRes) 
at ServiceStack.Razor.RazorFormat.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, Object dto) 
at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate) 
at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.SerializeToStream(IRequestContext requestContext, Object response, IHttpResponse httpRes) 
at ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponse(IHttpResponse response, Object result, ResponseSerializerDelegate defaultAction, IRequestContext serializerCtx, Byte[] bodyPrefix, Byte[] bodySuffix)

设置

Error.cs

public class Error
{
    public int TrackingNumber { get; set; }

    public string ClientName { get; set; }
}

[Route("/Errors", "GET")]
[Authenticate]
public class Errors : List<Error>
{

}

service.cs

public class ErrorService : Service
{
    public object Get(Errors request)
    {
        var fakeError = new Error(){ TrackingNumber=1, ClientName="Test"};
        var fakeErrorList = new Errors();
        fakeErrorList.Add(fakeError);
        return fakeErrorList;
    }
}

errors.cshtml

@inherits ViewPage<Errors>

<div>
    @Model.Count
</div>

1 个答案:

答案 0 :(得分:1)

因此,我对错误的唯一解释是将Errors类反序列化为View的过程不喜欢您从List继承。您应该能够从IList继承,但是您需要实现整个接口。根据{{​​3}}和this,从List继承似乎不是一个好主意。如果你想从List继承,我能想到的最简单的解决方案是将它转换为你服务中的一个数组,让你的视图继承自Error []。

public class ErrorService : Service
{
    public object Get(Errors request)
    {
        var fakeError = new Error(){ TrackingNumber=1, ClientName="Test"};
        var fakeErrorList = new Errors();
        fakeErrorList.Add(fakeError);
        return fakeErrorList.ToArray();
    }
}

errors.cshtml

@inherits ViewPage<Error[]>

<div>
    @Model.Count()
</div>