The Web Api is working fine in the localhost but when I upload the file in the production server, the app is working fine but the api is returning empty. Here is the WebApiConfig.cs modified code.
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 }
);
}
And Here is the 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);
}
I tried several fixes in web.config file
<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>
and here is the RAW json output that I get in HttpRequester(A firefox addon for testing APIs)
-- 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
[]
EDIT
The localhost version of Json response has following header
-- 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
I suspect on few things.
1) Json Serializer issue.
2) Type of Server (It doesn’t provide all the controls in the cPanel like most hosting servers do. That is why I guess, I need to check it on a different server)
Edit
However, It returns the sample string from the production server.
[HttpGet]
[Route("api/sample")]
public IHttpActionResult Sample()
{
string xyz = "Something";
return Ok(xyz);
}
3
Answers
Many Thanks to Babak Fakhriloo and Parv Sharma for pointing me in the right direction, I came to realize that my query was depending on a table which was going empty due to an internal bug.
The query was depending on ApiAccessName which was not saved for some reason.
I think you need to chain .ToList() on your .Select()
add yes of course null, u didnt execute your query, cheers