ServiceStack:使用HttpResponse时出现Null异常

时间:2013-01-13 23:13:27

标签: servicestack

我一直很喜欢ServiceStack中的干净约定,但是我最近遇到了一个棘手的问题。用户加载“创建”页面以创建“广告”,然后发布表单数据。术语服务器应该处理发布的数据并在成功时重定向用户。

我用调试器运行它。 POST方法完成且没有错误,但服务器在服务完成后始终返回错误:

Object reference not set to an instance of an object. AT 

CompiledRazorTemplates.Dynamic.dfdcdedabeabca.Execute() 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 ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.<>c__DisplayClass1.<SerializeToStream>b__0(IViewEngine x) at System.Linq.Enumerable.Any[TSource](IEnumerable 1来源,Func 2 predicate) at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.SerializeToStream(IRequestContext requestContext, Object response, IHttpResponse httpRes) at ServiceStack.Common.Web.HttpResponseFilter.SerializeToStream(IRequestContext requestContext, Object response, Stream responseStream) at ServiceStack.Common.Web.HttpResult.WriteTo(Stream responseStream) 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)

我把代码煮到了这个以隔离问题:

    [Route("/ad/create", Verbs = "GET")]
public class AdCreateViewModelCriteria
{
}

[Route("/ad/create", Verbs = "POST")]
public class AdCreate
{
    //some things
}

public class AdCreateService:Service
{
//the error occurs AFTER this method is complete
    public object Post(AdCreate ac)
    {
        try
        {
            //do some stuff
            return new HttpResult()
                       {
                           StatusCode = HttpStatusCode.Redirect,
                           Headers ={{HttpHeaders.Location, "/home"}}
                       };
        }
        catch (Exception e)
        {
            return new HttpResult(e)
                       {
                           StatusCode = HttpStatusCode.InternalServerError
                       };
        }
    }


    [DefaultView("adcreate")]
    public object Get(AdCreateViewModelCriteria criteria)
    {
        return AdCreateViewModel.Load(criteria,Db);
    }
}

0 个答案:

没有答案