【发布时间】:2017-11-01 05:24:23
【问题描述】:
Web Api 在 localhost 中运行良好,但是当我在生产服务器中上传文件时,应用程序运行良好,但 api 返回为空。这是WebApiConfig.cs修改后的代码。
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
var settings = config.Formatters.JsonFormatter.SerializerSettings;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.Formatting = Formatting.Indented;
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("text/html"));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
这里是 Api 控制器 (AppsController.cs)
[HttpGet]
[Route("api/{plat}/{lang}")]
public IHttpActionResult AppsByPlatformAndLanguage(string plat, string lang)
{
var apps = _context.Apps
.Include(p => p.AgeGroup)
.Include(p => p.ProductCode)
.Include(p => p.Language)
.Include(p => p.Platform)
.Where(p => p.Platform.ApiAccessName == plat && p.Language.ApiAccessName == lang)
.Select(p => new
{
p.Name,
languageName = p.Language.Name,
platformName = p.Platform.Name,
productCode = p.ProductCode.Name,
p.StoreId,
p.StoreURL,
});
return Json(apps);
}
我在 web.config 文件中尝试了几个修复
<sessionState mode="Off"></sessionState>
<modules runAllManagedModulesForAllRequests="true">
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />
<remove name="ExtensionlessUrl-Integrated-4.0"/>
<remove name=" ExtensionlessUrl-ISAPI-4.0_32bit "/>
</handlers>
这是我在 HttpRequester(用于测试 API 的 firefox 插件)中得到的 RAW json 输出
-- response --
200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2
Connection: keep-alive
Keep-Alive: timeout=15
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
Set-Cookie:
AspNet.ApplicationCookie=nyZeFxJOu6HOLGVXW4JS3s0B1wmOLBhd
XTSAv9SyDYSXyTTnf7XFnEJ59hJ0kWPKr9Z8u18-rOnYmpwaQsGPnHH4LcsIqRsD
AqK7lI8w1pcFdaGtm07y1WFVURGnzm88cFDKZIZwNriTYllo972jqbbA3asdfDFLJadsfYad
fasdfas867asadfMCZ_q4BCrha82fLOpeP671R_QpUbbrwYE_xVLQgDRNW
cIXY-f1aEQtbB7jOHu5n_jJnSOhP3P3Tnhr7kNWVnEEyzlM9GrT_eYxc1V8mm4pGrpUvinS
QtKVRXDfUWsrexR4VLthul3IBcpRP5JgQxAAtPMp5soFgGAVa3D5IvVR6AGlzFgUTzy1
wblvLWftbOfMuo4h1rws5Z5eUgDuZ7gAnLh8X7XErGHw8e6AxGitVNQGntqXtCDHCq
TxQG03t5rhDpZeUST-IVPGZguz60qkR4jtlo1uyDUjtpMHNkFPJNWvAfQ51w
5eaQKuHHZRIU1On9jGeFvq;
path=/; expires=Wed, 15-Nov-2017 04:25:08 GMT; HttpOnly
X-Powered-By: ASP.NET
Date: Wed, 01 Nov 2017 04:25:07 GMT
[]
编辑 Json 响应的 localhost 版本具有以下标头
-- response --
200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 1015
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?pcTmF2ZWVkXE1hcmtldGluZ1xNYXJrZXRpbmdcYXBpXGlvc1xlbg==?=
X-Powered-By: ASP.NET
Date: Wed, 01 Nov 2017 06:10:39 GMT
我怀疑一些事情。
1) Json 序列化程序问题。 2)服务器类型(它不像大多数托管服务器那样提供cPanel中的所有控件。这就是我猜的原因,我需要在不同的服务器上检查它)
编辑
但是,它从生产服务器返回示例字符串。
[HttpGet]
[Route("api/sample")]
public IHttpActionResult Sample()
{
string xyz = "Something";
return Ok(xyz);
}
【问题讨论】:
-
看来路由没问题,否则你会得到错误而不是 200 Ok。您是否在返回 json 行上设置了断点?代码到达那里了吗?
-
是的,代码到达那里。它在 localhost 中运行良好。
-
我建议您在调用 _context.Apps 之前和之后在您的操作中添加一些日志记录,您是否尝试过使用 api 返回一个简单的字符串?
-
确保生产服务器中有数据。我知道这听起来很傻,但是在使用 diff 数据库进行调试和生产时会发生这种情况
-
@ToughGuy 所以如果它可以返回 json。您的 EF 查询可能存在问题,特别是当它没有引发任何异常时。
标签: json asp.net-mvc api json.net