Breeze.js with ASP MVC5 and AngularJS

angularjs asp.net-mvc breeze entity-framework-6

Question

I'm looking into creating an ASP MVC5 project (NOT an WebApi, I have to stick with ASP.MVC) with Breeze.js and AngularJS but I'm facing all kind of problems as the documentation is not complete on achieving this (all new examples are made with WebApi). I created the an MVC controller though the package I found on NuGet (Breeze.WebApi2), so I manage to compile but I'm not sure it actually works anyhow, I have a feeling it's not doing the magic I'm suppose to get. If I go to http://localhost/breeze/MyBreeze/MetaData, I get the JSON object of it, cool that part works, though if I try to get Attendees with this http://localhost/breeze/MyBreeze/Attendees then the only thing I get is the SQL Query and so not JSON data, which I guess I'm suppose to get..am I? Does Breeze.WebApi2 and [BreezeController] work with ASP MVC5 (again not WebApi)?
Here is my MVC5 Controller:

using Breeze.ContextProvider;
using Breeze.ContextProvider.EF6;
using Breeze.WebApi2;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TemplateBreezeAngularMVC5.Models;

namespace TemplateBreezeAngularMVC5.Controllers
{
    [BreezeController]
    public class MyBreezeController : Controller
    {
        static readonly TimeSpan RefreshRate = TimeSpan.FromMinutes(60);
        private static readonly object Locker = new object();
        static DateTime _lastRefresh = DateTime.Now; // will first clear db at Now + "RefreshRate" 
        // static DateTime lastRefresh = DateTime.MinValue; // will clear when server starts

        private readonly EFContextProvider<ConferenceContext> _contextProvider = new EFContextProvider<ConferenceContext>();

        public MyBreezeController()
        {

        }

        [HttpGet]
        public string Metadata()
        {
            return _contextProvider.Metadata();
        }

        [HttpGet]
        public IQueryable Attendees()
        {
            return _contextProvider.Context.Attendees;
        }

        [HttpGet]
        public IQueryable Presentations()
        {
            return _contextProvider.Context.Presentations;
        }

        [HttpGet]
        public IQueryable Speakers()
        {
            return _contextProvider.Context.Speakers;
        }

        [HttpPost]
        public SaveResult SaveChanges(JObject modifications)
        {
            return _contextProvider.SaveChanges(modifications);
        }
    }
}

Here is also my Angular code, very basic, I just want the query to work and return the data though it fail and returns the query instead, something like SELECT [Extent1].[Id] AS [Id], ... but I guess it's because the MVC Controller isn't working correctly. Here is my Angular code anyway:

breeze.config.initializeAdapterInstance('modelLibrary', 'backingStore', true);
//Q.stopUnhandledRejectionTracking();

var myApp =
    angular
        .module('myApp', ['ngRoute', 'breeze.angular', 'breeze.directives'])
        .controller("SpeakersCtrl", function ($scope) {
            var manager = new breeze.EntityManager('/breeze/MyBreeze');

            var query = new breeze.EntityQuery.from("Speakers");
            manager.executeQuery(query).then(function (data) {
                $scope.results = data.results;
                $scope.$apply();
            }).fail(function (e) {
                alert(e);
            });
        });

Then my NuGet packages installed are AngularJS, Breeze.Client, WebApi2.EF6, Breeze.Angular.Directives, Breeze.Angular, Breeze.Server.ContextProvider.EF6, Breeze.Server.ContextProvider, Breeze.Server.WebApi2, EntityFramework 6.0, jQuery, Json.NET, Q
I also tried to get the old HotTowel MVC4 but it's missing lot of things and didn't get it to work.

Edit
It's worth to know that the reason of why I want to use ASP MVC is that my company force me to use it and also because I will NOT make a SPA.

1
0
6/18/2014 11:21:13 PM

Popular Answer

To answer my own question, with the help of Steeve Schmitt and Ward, I came up to the conclusion that I can keep my project as an ASP MVC but keep the BreezeController as a WebApi Controller. There is no problem into mixing MVC Controller with WebApi Controller. I had to change this:

public class MyBreezeController : Controller

to this:

public class MyBreezeController : ApiController

and also make sure to also have the BreezeWebApiConfig.cs part of your project with the routing properly setup. And voilà!!!

2
6/20/2014 5:33:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow