Jason Mobley
Jason Mobley

Reputation: 21

Web Api OData Controller returns blank page no error

I'm developing a library of OData queries using Web Api and ODataController. When I go to run my api from a web browser it returns nothing. I don't get an error of any kind. I can debug in Visual Studio and see clearly that the method runs and successfully returns my results as an IQueryable<>. Somewhere under the hood it's discarding my data. Has anyone else seen or encountered this? I've included my code below for reference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData;
using Epm.Core.Model;
using System.Web.Http.OData.Query;
using Epm.Data.Access;
using Epm.Service.Assemblers;

namespace Epm.Service.Web.Controllers.OData
{
    public class SearchActionsController : ODataController
    {
        private readonly EpmEntities context = new EpmEntities();

        [Queryable(AllowedQueryOptions=AllowedQueryOptions.All)]
        public IQueryable<ActionStepDisplay> Get(int planId, int? factorId, bool? showArchived)
        {
            var user = this.GetCurrentUser();

            var results = (from p in context.SearchActions(user.SessionId, planId, factorId, showArchived, 1, null)
                           select p).ToModel().ToArray();

            return results.AsQueryable();
        }

        protected override void Dispose(bool disposing)
        {
            context.Dispose();
            base.Dispose(disposing);
        }
    }
}

My configuration:

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Epm.Core.Model.ActionStep>("SearchActions");

Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);

Upvotes: 2

Views: 2445

Answers (2)

Linoy
Linoy

Reputation: 1395

You are returning ActionStepDisplay from your method, but in builder you specified ActionStep as the entity.

May be Not Acceptable (406) in the response header

Upvotes: 5

0909EM
0909EM

Reputation: 5027

Possibly the problem lies with the MediaFormatter, which gets called after the controller has finished. When a media type formatter encounters a reference loop (where object A reference B and B references A) then you need to tell the media type formatter how to handle that, so in the Json Media Type Formatter you do something like...

json.SerializerSettings.PreserveReferencesHandling = 
  Newtonsoft.Json.PreserveReferencesHandling.All;

See Documentation Here

I'd recommend you use Fiddler to see what is actually going on. You say your not getting a response in the browser, so what HTTP code is returned? You can use Fiddler to find out...

Upvotes: 0

Related Questions