8

I am new to AngularJS and want to use it for our new project based on ASPNET MVC. I want AngularJS to manage the views ( it will be hybrid SPA, some pages normal MVC generated views). But I am in fix to decide what should I choose at the server end. i.e. ServiceStack/WebApi/MVC Actions ? There are examples in the web for WebAPI and regular ASPNET MVC, but couldn't find any SS+Angular examples. Could you give me an example project with SS+Angular( how to manage routing, prevent the views( html files) from opening directly by the user etc).

3
  • 2
    ServiceStack provides a RESTful service as a backend. Angular can consume RESTful services to create a frontend. There's no actual coupling between the two. Commented Sep 4, 2013 at 19:34
  • I was thinking which one will be better in terms of performance ( probably SS because its JSON serializer is the fastest according to their site. That's why I was looking for post which also have a working example project Commented Sep 4, 2013 at 19:41
  • I am in the exact same situation so contact me if you want to trade ideas. one resource with articles and code I am using is here github.com/Wintellect/Angular-MVC-Cookbook Commented Sep 25, 2013 at 17:47

5 Answers 5

8

A few months back I put together a Demo project (https://github.com/paaschpa/ordersDemo) for Chicago Code Camp 2013. The sample site on AppHarbor seems to be down (I got the AppHarbor site working but all my 'production configs' are in the GitHub repo. I can never get the config/settings right between GitHub and them) but I think the code resembles what you're asking for. It uses AngularJS (probably not the best example of it), .NET MVC w/ServiceStack hosted at /api. It also uses Twitter BootStrap, Redis Pub/Sub and SignalR...probably smashed too much stuff into this project/example. If you can get Redis installed (https://github.com/dmajkic/redis/downloads) and you change the redisUrl to localhost:6379 in the web.config file you should be able to get it running locally.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks this is what I was looking for. This is one hell of a cool sample for learning, includes all that I was looking for. Thanks again for your help
Cool. It's easier to understand the example/code when combined with the talk. This was also my first go at AngularJS, Redis and SignalR so the usage may not follow 'best practices'. But, it is/was a fun toy project.
I wasn't looking for best practices, rather a working sample with SS, but I got some bonus. This will be a great help for starters like me to have a go at AngularJS @paaschpa
5

I use ServiceStack + ASP.NET MVC + Angular in my project.

Once ServiceStack installed (pretty easy with nugget package), call ServiceStack Rest WS is very simple with angular in a service:

GetById: function (movieId) {
    var url = root + 'api/movie/' + movieId;
    var promise = $http({ method: 'GET', url: url }).then(function (response) {
        return response.data;
    });

    return promise;
}, ...

In ServiceStack I Use DTO and ViewModel like this :

#region MovieDTO
[Api("GET Movie by Id")]
[Route("/movie/{Id}")]
public class MovieDTORequest
{
    public int Id { get; set; }
}

public class MovieDTOResponse
{
    public MovieViewModel Movie{ get; set; }
}

#endregion

And to finish my service :

private MovieBusiness _movieBusiness= (MovieBusiness )UnityHelper.BusinessResolve<Movie>();
public object Get(MovieDTORequest request)
{
    Movie movie = _movieBusiness.GetById(request.Id);
    MovieViewModel movieViewModel = MovieAdapter.ToViewModel(movie);

    return new MovieDTOResponse { Movie = movieViewModel };
}

Concerning routing, in my cas I use ASP.NET MVC route because I am more comfortable with it. So I have created a BaseController sending ServiceStack User to each View:

[ProfilingActionFilter]
public class BaseController : ServiceStackController<CustomUserSession>
{
    /// <summary>
    /// Surcharge de l'action pour charger la notification dans le ViewBag s'il y en a une et l'a marquer comme delivrée
    /// </summary>
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
        int Id = 0;
        UserViewModel user= new UserViewModel ();

        if (int.TryParse(base.UserSession.UserAuthId, out Id))
        {
            user= new UserViewModel ()
            {
                id = Convert.ToInt32(base.UserSession.UserAuthId),
                nom = base.UserSession.DisplayName,
                roles = base.UserSession.Roles != null ? string.Join(",", base.UserSession.Roles.ToArray()) : string.Empty
            };
        }
        ViewBag.User= user;
    }

Next if you need to pass a ViewModel direcly to a angular Controller you can do this :

@model AddictLive.Core.ViewModel.Mobile.ViewModels.MovieViewModel
@using Newtonsoft.Json

<div ng-controller="MovieController" ng-init="init(@Html.Raw(JsonConvert.SerializeObject(Model)))">
     ...
</div>

Sample of init() method in the angular controller :

$scope.init = function (movieViewModel) {
    $scope.property1= movieViewModel.property1;
    $scope.property2= movieViewModel.property2;
};

I simplified all my examples to make it easy to understand but if you need more explanation let me know

Comments

2

Would this be what you are looking for?

https://github.com/Wintellect/Angular-MVC-Cookbook

2 Comments

Does it have SS+Angular examples? @Silas I need ServiceStack with AngularJS example project
I don't know if it specifically had Service Stack, but it does have routing and should at least give you a good idea about how to integrate angular into an MVC project. If I recall correctly, service stacks are just normal stacks, for all intents and purposes, with a specific API. Am I wrong?
2

The SocialBootstrap project contains an decent full stack setup, it uses backbone.js & underscore.js not angular though - it may help to read through it.

https://github.com/ServiceStack/SocialBootstrapApi

Although, in this example the service layer is tightly coupled to the front-end as they are both included in the same project. This is something I would try and avoid for a larger SPA.

Comments

2

I'm doing a SS + AngularJs + SignalR and I can say to you I have no regret about it, personally I feel is very straight forward the framework aims to just the usage of the plugins and your IoC (in my case I use SimpleInjector).

I researched angular and SS separatelly since REST calls in angular can be BackEnd agnostic but you still need to figure it out things like integrate security between the front end and the back end, routing, etc

you can find a small demo at razorRockstarts which uses SS + angularJs you can take a look to this post and this other as well.

I hope that helps

1 Comment

Thanks for the info, I will have a look at it now @Pedro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.