我可以通过 Javascript 使用“/api/X”路径调用我的服务。(后动词)
我可以使用 client.Get(serviceUrl) //client is JsonServiceClient在没有请求对象的情况下调用相同的服务
但是 client.Send(X) 不起作用。我收到奇怪的 404 NotFound 响应?
我错过了什么吗?我该如何调试问题?
到现在为止的费用是5小时!
清单
- X 类有两个字符串属性(没有枚举或自定义类型)
- X 和 XResponse 具有 DataContract 和 DataMember 属性(试用版)
代码:
在 AppHost.cs 中
base.SetConfig(new EndpointHostConfig
{
GlobalResponseHeaders =
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
},
AppendUtf8CharsetOnContentTypes = new HashSet<string> { ContentType.Html },
DebugMode = true, //Show StackTraces in service responses during development
LogFactory = new ElmahLogFactory(new Log4NetFactory())
});
//Set JSON web services to return idiomatic JSON camelCase properties
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
//Configure User Defined REST Paths
Routes.Add<JobRequest>("/job");
在 Model.cs 中
[DataContract]
public class JobRequest : IReturn<JobRequestResponse>
{
[DataMember]
public string JobRequestEnum { get; set; }
[DataMember]
public string JobData { get; set; }
}
[DataContract]
public class JobRequestResponse : IHasResponseStatus
{
[DataMember]
public string Message { get; set; }
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
在 JobService.cs 中
public class JobService : ServiceStack.ServiceInterface.Service
{
public JobRepository Repository { get; set; }
public object Any(JobRequest request)
{
return new JobRequestResponse() { Message = "ok" };
}
}
在 Javascript.js 中 // 它可以工作
$.ajax({
type: 'POST',
url: "/api/job",
data: '{ "jobRequestEnum": "test", "jobData": "test" }',
dataType: "json",
contentType: "application/json",
success: function (res) {
debugger;
}
});
在 CallJob.js 中 // 它不起作用
string serviceUrl = ConfigurationManager.AppSettings["serviceUrl"];
using (JsonServiceClient client = new JsonServiceClient(serviceUrl))
{
var request = new JobRequest() { JobData = "test", JobRequestEnum = "test" };
var response = client.Send<JobRequestResponse>(request);
}