ServiceStack'访问被拒绝'再次,和其他问题

时间:2013-09-30 18:32:00

标签: c# jquery web-services servicestack

我以为我已将我的访问权限问题解决到我的ServiceStack网络服务in this question,但我现在得到了相同的“访问被拒绝”错误,即使在该解决方案中找到了修复程序并且无法解决我自己解决它们。

以下是我对此的看法: 我有一个Web服务,我使用POST方法发送数据。我也有一个GET方法返回与POST相同的响应类型,并且工作正常。据我所知,该请求在失败之前甚至没有到达ServiceStack。尽管我在响应标头中使用了CorsFeature插件,但响应中没有Access-Control标头。我无法弄清楚为什么它没有得到任何ServiceStack代码虽然......一切似乎都正确设置。顺便说一句,当我尝试DELETE操作时,我从服务器获得“403 Forbidden,Write access is denied”错误,如果这有用的话?

这是我的Global.asax(相关部分):

public class AppHost : AppHostBase
{
    public AppHost() : base("RMS Citations Web Service", typeof(CitationService).Assembly) { }

    public override void Configure(Container container)
    {
        SetConfig(new EndpointHostConfig
        {
            DefaultContentType = ContentType.Json,
            ReturnsInnerException = true,
            WsdlServiceNamespace = "http://www.servicestack.net/types"
        });

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

        container.RegisterAutoWired<CitationRequest>();

        // Not sure I need this - is it necessary for the Funq container?
        using (var addCitation = container.Resolve<CitationService>())
        {
            addCitation.Post(container.Resolve<CitationRequest>());
            addCitation.Get(container.Resolve<CitationRequest>());
            addCitation.Delete(container.Resolve<CitationRequest>());
        }
    }
}

protected void Application_Start(object sender, EventArgs e)
{
    new AppHost().Init();
}

这是我的请求和响应类:

[Route("/citations", "POST, OPTIONS")]
[Route("/citations/{ReportNumber_Prefix}/{ReportNumber}/{AgencyId}", "GET, DELETE, OPTIONS")]
public class CitationRequest : RmsData.Citation, IReturn<CitationResponse>
{
    public CitationStatus Status { get; set; }
}

public enum CitationStatus
{
    COMP,
    HOLD
}

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 Post(CitationRequest citation)
    {
        var response = new CitationResponse { Accepted = false };

        if (string.IsNullOrEmpty(citation.ReportNumber))
        {
            response.Accepted = false;
            response.Message = "Report number was empty, so no data was sent to the web service.";
            return response;
        }

        try
        {
            response.ActivityId = Repository.CreateCitation(citation.ReportNumber, citation.ReportNumber_Prefix, citation.ViolationDateTime, citation.AgencyId, citation.Status);
            response.Accepted = true;
        }
        catch (Exception ex)
        {
            response.Accepted = false;
            response.Message = ex.Message;
            response.RmsException = ex;
        }

        return response;
    }

    public CitationResponse Get(CitationRequest citation)
    {
        var citationResponse = new CitationResponse();
        if (string.IsNullOrEmpty(citation.ReportNumber))
        {
            citationResponse.Accepted = false;
            citationResponse.Message = "Error occurred passing citation data to web service.";
            return citationResponse;
        }

        var isDuplicate = Repository.IsDuplicateReportNumber(citation.AgencyId, citation.ReportNumber, citation.ReportNumber_Prefix);
        citationResponse = new CitationResponse
        {
            Accepted = isDuplicate,
            Message =
                isDuplicate ? "Report Number already exists in database." : "Report Number has not yet been used."
        };

        return citationResponse;
    }

    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 RMS successfully.";
            }
            else
            {
                citationResponse.Accepted = false;
                citationResponse.Message = "Citation NOT deleted from RMS.  Check exception for details.";
            }
        }
        catch (Exception ex)
        {
            citationResponse.Accepted = false;
            citationResponse.Message = ex.Message;
            citationResponse.RmsException = new Exception(ex.Message);
            throw;
        }

        return citationResponse;
    }
}

最后,这是我如何将数据发送到Web服务。它始终适用于错误块:

SendCitationToDb: function (cit, callback) {
    $.ajax({
        type: "POST",
        url: Citations.DataServiceUrl + "citations",
        data: JSON.stringify(cit),
        contentType: "application/json",
        dataType: "json",
        success: function(data) {
            if (!data.Accepted) {
                Citations.ShowMessage('Citation not added', 'Citation not added.  Error was: ' + data.Message, 'error');
            } else {
                ActivityId = data.ActivityId;
                callback(data);
            }
        },
        error: function(errMsg) {
            Citations.ShowMessage('Citation not added', 'Citation not added.  Error was: ' + errMsg.statusText, 'error');
        }
    });
}

以下是Chrome开发工具的示例输出。首先是这项服务的良好反应(GET): enter image description here

在IIS ::中添加aspnet_isapi.dll路径到通配符后

更新响应

更新2 - 这是POST响应。请求显示它是POST方法,但代码跳转到jQuery ajax函数中的错误块。在开发工具中,我看到这一行: enter image description here

然后我点击它并获取这些请求和响应标头: enter image description here

不知道还有什么可以寻找的 - 我知道它说状态代码200好了,但我认为这仅仅适用于飞行前选项......?不确定,但它没有从服务返回有效的响应。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

看起来WebDav正在处理和拒绝您的请求。您可以尝试disabling WebDav查看是否有帮助。