我对 Restful 服务相当陌生,我刚刚实现了测试代码来让 ServiceStack Restful 服务与 Swagger 插件一起工作,这让我想到了我的问题......
在 swagger-ui/index.html 中有一个用于“api_key”的字段。我知道变量名称是嗯...变量,我也可以随意设置它,但我有点搞不清楚它的用途以及我是否应该使用它。
此外,如果我确实使用它,servicestack 如何在服务器端向我呈现该值?
这是我从文档中启动并运行的测试服务...
[Api("Hello Web Services")]
[Route("/Hello", Summary = @"Noel's ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes")]
[Route("/Hello/{name}", Summary = @"N031'5 ServiceStackSwagger thingy", Notes = "Some more info in here cause these are notes", Verbs="GET,POST" )]
public class Hello
{
[ApiMember(Name = "Name", Description = "This is a description", ParameterType = "path", DataType = "string", Verb="GET,POST")]
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
最佳答案
为了回答我自己对 Esker 的后续请求,这里是如何使用 API key ……
public class HelloService : Service
{
public object Any(Hello request)
{
string api_key = this.Request.Headers["api_key"];
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
但还需要一些额外的 javascript 将其包含在 header 中(在 swagger-ui/index.html 内)...
$(function () {
$.ajaxSetup({
beforeSend: function (jqXHR, settings) {
jqXHR.setRequestHeader("api_key", $("#input_apiKey").val());
}
});
});
我在这个问题的答案中找到了...
How to get Swagger to send API key as a http instead of in the URL
关于c# - 什么是 'api_key' 以及如何正确使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18205134/