ASP.net MVC5 EF6 Api在生产服务器上返回空字符串

时间:2017-11-01 05:24:23

标签: json asp.net-mvc api json.net

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 Controller(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中获得的RAW json输出(用于测试API的firefox插件)

 -- 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 Serializer问题。 2)服务器的类型(它没有像大多数托管服务器那样提供cPanel中的所有控件。这就是为什么我猜,我需要在不同的服务器上检查它)

修改

但是,它从生产服务器返回示例字符串。

    [HttpGet]
    [Route("api/sample")]
    public IHttpActionResult Sample()
    {

        string xyz = "Something";
        return Ok(xyz);
    }

3 个答案:

答案 0 :(得分:0)

我认为您需要在.Select()

上链接.ToList()

答案 1 :(得分:0)

非常感谢Babak Fakhriloo和Parv Sharma指出我正确的方向,我开始意识到我的查询取决于由于内部错误而变空的表。

该查询取决于ApiAccessName,由于某种原因未保存。

.Where(p => p.Platform.ApiAccessName == plat && p.Language.ApiAccessName == lang)

答案 2 :(得分:0)

   [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,
     }).ToList();

    return Json(apps);
}

添加yes当然为null,你没有执行你的查询,欢呼