【问题标题】:ServiceStack GET action gets null objectServiceStack GET 操作获取空对象
【发布时间】:2013-10-04 04:50:22
【问题描述】:

我正在查询我最近不得不使用的 ServiceStack 服务以使 POST 操作正常工作,现在当我调用我的 GET 操作时,JSON 对象不再被传入,对于我的一生来说,我不知道我做了什么来破坏它...

这是我的请求标头:

Request Url: http://internalserver:8181/citations
Request Method: GET
Status Code: 200
Params: {}

这是我的全局配置:

    public override void Configure(Container container)
    {
        container.RegisterAutoWired<CitationRequest>();
        container.RegisterAutoWired<Citation>();

        //Not sure I need these usings...?
        using (var addCitation = container.Resolve<CitationService>())
        {
            addCitation.Post(container.Resolve<CitationRequest>());
            addCitation.Get(container.Resolve<CitationRequest>());
            addCitation.Delete(container.Resolve<CitationRequest>());
        }

        Plugins.Add(new CorsFeature());
        RequestFilters.Add((httpReq, httpRes, requestDto) =>
        {
            if (httpReq.HttpMethod == "OPTIONS")
                httpRes.EndRequestWithNoContent(); //   extension method                    
        });

        SetConfig(new EndpointHostConfig
        {
            DefaultContentType = ContentType.Json,
            ReturnsInnerException = true,
            DebugMode = true,
            AllowJsonpRequests = true,
            ServiceName = "SSD Citations Web Service",
            WsdlServiceNamespace = "http://www.servicestack.net/types",
            WriteErrorsToResponse = true
        });
    }

这是我的服务:

[Route("/citations/{ReportNumber}/{ReportNumber_Prefix}/{AgencyId}", "GET, DELETE, OPTIONS")]
[Route("/citations", "GET, POST, DELETE, OPTIONS")]
public class CitationRequest : IReturn<CitationResponse>
{
    public string ReportNumber { get; set; }
    public int ReportNumber_Prefix { get; set; }
    public string AgencyId { get; set; }
    public DateTime ViolationDateTime { get; set; }
    public CitationStatus Status { get; set; }
}
public class CitationResponse
{
    public bool Accepted { get; set; }
    public string ActivityId { get; set; }
    public int ParticipantId { get; set; }
    public string Message { get; set; }
    public Exception RmsException { get; set; }
}
public class CitationService : Service
{
    public Repository Repository { get { return new Repository(); } }
    public CitationResponse Get(Citation citation)
    {
        var isDuplicate = Repository.IsDuplicateReportNumber(citation.AgencyId, citation.ReportNumber, citation.ReportNumber_Prefix);
        var citationResponse = new CitationResponse
           {
                Accepted = isDuplicate,
                Message = isDuplicate ? "Report Number already exists in db." : "Report Number has not yet been used."
            };
        return citationResponse;
    }

    public CitationResponse Post(CitationRequest request)
    {
        var response = new CitationResponse { Accepted = false };
        if (string.IsNullOrEmpty(request.ReportNumber))
        {
            response.Accepted = false;
            response.Message = "No data sent to service.  Please enter data in first.";
            return response;
        }
        try
        {
            response.ActivityId = Repository.CreateCitation(request.ReportNumber,     request.ReportNumber_Prefix, request.ViolationDateTime, request.AgencyId, request.Status);
            response.Accepted = true;
        }
        catch (Exception ex)
        {
            response.Accepted = false;
            response.Message = ex.Message;
        }
        return response;
    }

    public CitationResponse Delete(CitationRequest citation)
    {
        var citationResponse = new CitationResponse();
        try
        {
            if (Repository.DeleteCitation(citation.ReportNumber, citation.AgencyId, citation.ReportNumber_Prefix))
            {
                citationResponse.Accepted = true;
                citationResponse.Message = "Citation removed from db successfully.";
            }
            else
            {
                citationResponse.Accepted = false;
                citationResponse.Message = "Citation NOT deleted.";
            }
        }
        catch (Exception ex)
        {
            citationResponse.Accepted = false;
            citationResponse.Message = ex.Message;
            citationResponse.RmsException = new Exception(ex.Message);
            throw;
        }

        return citationResponse;            
    }
}

我的 POST 方法完全填充了我传递给它的 JSON 对象,所有值都在那里并且可用。我的 GET 操作中的同一个 EXACT 对象的所有属性都具有空值。这是一个示例负载:

{"ReportNumber":"TEST275455",
 "ReportNumber_Prefix":"2013",
 "AgencyId":"BBC",
 "Status":"COMP",
 "ViolationDateTime":"9-21-2013 12:00:00"}

我被难住了,拔出头发想弄清楚为什么 Get 与 Post 不同??非常感谢您的帮助!

【问题讨论】:

    标签: json servicestack


    【解决方案1】:

    您已将路由信息添加到 CitationRequest 请求 DTO:

    [Route("/citations/{ReportNumber}/{ReportNumber_Prefix}/{AgencyId}", "GET, DELETE, OPTIONS")]
    [Route("/citations", "GET, POST, DELETE, OPTIONS")]
    public class CitationRequest : IReturn<CitationResponse>
    

    建议 Get 提供它,但您的实现需要 Citation 代替?

    public CitationResponse Get(Citation citation)
    

    Post 的声明使用了正确的声明:

    public CitationResponse Post(CitationRequest request)
    

    【讨论】:

    • 谢谢mythz,我解决了这个问题,但我的JSON 对象在进入Get 操作时仍然为空。另外,我的 DELETE 操作现在返回不允许的 405 方法...??当我查看 DELETE 的标头时,允许的动词不包括 DELETE,但我已经把它放在了整个代码中,到处都是我放置其他动词的地方,那么它怎么能不将它包含在响应中呢?我越是尝试解决这个问题,它就越糟糕......
    • @Eddie 查看405 DELETE issue 的答案。请求 DTO 绝不应该是 null,但根据您的请求标头,它应该是,因为您没有在 QueryString 或匹配的 PathInfo 中填充任何请求 DTO 属性。
    • 感谢有关 405 DELETE 的信息 - 我将尝试使用 web.config 条目。不知道如何实现 IIS 更改,因为 ServiceStack 没有文件扩展名......另外,DELETE 已经工作了几个星期,然后我今天做了一些事情来搞砸它。关于请求 DTO,是的,你是对的,它不是空的,但它是空的。但是,如果我在“/引文”处调用服务并在上面的有效负载中传递这个 JSON 对象,它不应该用这些值填充 CitationRequest 吗?这就是它在 POST 中的工作方式,并且曾经与 DELETE 一起使用 - 为什么不与 GET 一起使用?
    • @Eddie,如果您使用 jQuery Ajax 调用,我认为在 GET 中您应该有一个带有请求参数的 URL。 Get 不采用 json 对象。如果我错了,mythz可以纠正我,否则请使用POST。
    • 什么有效载荷? GET 不能有有效载荷,您需要使用 url,或者带有查询字符串或 pathInfo。您还可以使用 JSVFormat 在 QueryString 上传递复杂对象,请参阅the wiki docs
    猜你喜欢
    • 2018-07-07
    • 1970-01-01
    • 2013-01-06
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多