2

我正在努力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>
4

1 回答 1

1

因此,我对该错误的唯一解释是将 Errors 类反序列化为 View 的过程不喜欢您从 List 继承。您应该能够从 IList 继承,但是您需要实现整个接口。根据thisthis从 List 继承似乎不是一个好主意。如果您想从 List 继承,我能想到的最简单的解决方案是将其转换为 Service 中的 Array,并让您的视图从 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();
    }
}

错误.cshtml

@inherits ViewPage<Error[]>

<div>
    @Model.Count()
</div>
于 2013-04-09T04:15:54.017 回答