c# - ServiceStack 'Access is denied' 又来了,还有其他问题

标签 c# jquery web-services servicestack

我以为我已经解决了对 ServiceStack Web 服务的访问问题 in this question ,但我现在遇到了相同的“访问被拒绝”错误,即使在该解决方案中找到了修复程序,我也无法自行解决。

这是我的观点: 我有一个 Web 服务,我正在使用 POST 方法向其发送数据。我还有一个 GET 方法,它返回与 POST 相同的响应类型,并且工作正常。据我所知,请求在失败之前甚至没有到达 ServiceStack。响应中没有访问控制 header ,即使我在响应 header 中使用了 CorsFeature 插件。我不明白为什么它没有获取任何 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;
    }
}

最后,这是我将数据发送到网络服务的方式。它总是正确到错误 block :

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

更新在将 aspnet_isapi.dll 路径添加到 IIS 中的通配符后的响应::(已删除屏幕截图)

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

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

不确定还需要寻找什么 - 我知道它说状态代码 200 OK,但我认为这只是用于飞行前的选项...?不确定,但它不会从服务返回有效响应。

非常感谢您的帮助!

最佳答案

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

关于c# - ServiceStack 'Access is denied' 又来了,还有其他问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19101264/

相关文章:

c# - 为什么CefSharp显示 'Not allowed to load local resource: file:///C:/...'?

jQuery:如何创建元素然后将其包装在另一个现有元素周围?

jquery - 如何只选择图像的 "parent"div

jquery - 循环插件ie8的问题

c# - 从服务器获取数据到移动应用程序的最佳方式

c# - 调试器如何获取有关初始化为 null 的对象的类型信息?

c# - 如何在测试方法 Moq 中设置查询字符串的值

c# - 无法使用 File.Move 创建已存在的文件

javascript - 使用 $.ajax POST 将查询字符串参数传递给 WebService

c# - 使用 Emailmessage 类检索邮件正文