我以为我已经在这个问题中解决了对我的 ServiceStack Web 服务的访问问题,但我现在遇到了相同的“访问被拒绝”错误,即使在该解决方案中找到了修复程序并且无法在我的自己的。
这就是我要解决的问题:我有一个 Web 服务,我正在使用 POST 方法向它发送数据。我还有一个 GET 方法,它返回与 POST 相同的响应类型,并且工作正常。据我所知,该请求在失败之前甚至没有到达 ServiceStack。响应中没有 Access-Control 标头,即使我在响应标头中使用了 CorsFeature 插件。我不明白为什么它没有进入任何 ServiceStack 代码......一切似乎都设置正确。顺便说一句,当我尝试删除操作时,我从服务器收到“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)的良好响应:
在 IIS 中将 aspnet_isapi.dll 路径添加到通配符后更新了响应:: (已删除屏幕截图)
更新 2 - 这是 POST 响应。请求显示它是 POST 方法,但代码直接跳转到 jQuery ajax 函数中的错误块。在开发工具中,我看到了这一行:
然后我单击它并获取这些请求和响应标头:
不知道还要寻找什么 - 我知道它说状态代码 200 OK,但我认为这只是为了飞行前的选项......?不确定,但它不会从服务返回有效响应。
非常感谢您的帮助!