【发布时间】:2018-01-22 09:45:33
【问题描述】:
我正在与奇怪的情况作斗争。我正在从 postMan 向 ApiController 调用 void 方法,但得到了奇怪的结果。 ApiController 捕获请求并返回带有 3 个对象的结果,但邮递员以某种方式显示无法获得任何响应。虽然所有其他返回一个值的方法都可以正常工作,但问题却是返回多个对象。
ApiController:
[HttpGet]
[Route("api/documents/AllDocs/")]
public List<Document> AllDocs()
{
lock (_lock)
{
_documentsRepository = DocumentsRepository.Instance;
var result = _documentsRepository.GetDocuments();
return result;
}
}
文档存储库:
public List<Document> GetDocuments()
{
var documents = new List<Document>();
try
{
var db = new Context();
var docs = db.Document;
//.Include(x => x.DocumentItems)
//.Include(x => x.Page.Select(y => y.RegionCropper.Select(z => z.Cropper)))
//.Include(x => x.Page.Select(y => y.RegionCropper.Select(z => z.MinorCropper))).ToList();
return docs.ToList();
}
catch (Exception ex)
{
return documents;
}
}
邮递员:
网络配置:
public static class WebApiConfig
{
private static HttpSelfHostServer _server;
public static void Run(string port)
{
var config = new HttpSelfHostConfiguration($"http://localhost:{port}");//"http://localhost:8080");
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
//config.Routes.MapHttpRoute(
// "newdocument", "api/documents/newdocument/{document}",new { document = RouteParameter.Optional });
config.MaxReceivedMessageSize = int.MaxValue;;
config.MaxBufferSize = int.MaxValue;
_server = new HttpSelfHostServer(config);
_server.OpenAsync();
}
public static void Stop()
{
_server?.CloseAsync();
}
}
【问题讨论】:
-
你试过没有最后一个斜线
[Route("api/documents/AllDocs")]吗? -
@rene 它与斜线无关,因为方法被调用并返回值。
-
这是什么ApiController?您是否尝试过返回 ActionResult?
-
@Alex 我添加了 webconfig。不,我没有,但是返回一个对象的其他方法可以正常工作,因此该方法也应如此。那么为什么要改变返回类型呢?
-
@Chris 一切都在邮递员的本地机器上,除了这个之外的每个方法都根据需要返回,但问题是这样的,然后它 api 返回结果多个对象。标题中解释的所有内容请先阅读。
标签: c# request httpclient asp.net-apicontroller