orchard routes

Topics: Core, Customizing Orchard, General, Troubleshooting, Writing modules
Dec 4, 2012 at 8:16 AM

I've built an apiController which has 3 methods with the following signatures:

public string GetUserArticles(int usrId)
public string AttachArticleToUser(int usrId, int articleId)
public string DeleteArticleFromFavorites(int usrId, int articleId)

as you can see I have 2 methods with the same parameters

I want my route to be:

api/favorites/{action}/{userId}/{articleId}

without the module name,

with articleId as an optional parameter,

with actions as a parameter.

I created a HttpRoutes.cs file but the action and optional parameters are still missing, can you help?

 

 public IEnumerable<RouteDescriptor> GetRoutes()
        {
            return new[]   
            {
                new HttpRouteDescriptor 
                {
                    Name = "FavoriteArticles",
                    Priority = -10,
                    RouteTemplate = "api/favorites/{action}/{userId}/{articleId}",
                    Defaults = new {
                        area = "YIT.FavoriteArticles",
                        controller = "FavoriteArticles"
                    },
                }   
            };
        }

 

Coordinator
Dec 5, 2012 at 6:18 AM

PArameter name you used is usrId, not userId.

Dec 5, 2012 at 7:31 AM

Thanks, but even without his typo it doesn't seem to work, I'm getting the following error:

The controller for path '/OrchardLocal/api/favorites/GetArticles/12' was not found or does not implement IController.

 

my routes file is:

 

 

using System.Collections.Generic;
using Orchard.Mvc.Routes;
using Orchard.WebApi.Routes;
using System.Web.Routing;
using System.Web.Mvc;

namespace YIT.FavoriteArticles
{
    public class Routes : IHttpRouteProvider
    {

        public void GetRoutes(ICollection<RouteDescriptor> routes)
        {
            foreach (RouteDescriptor routeDescriptor in GetRoutes())
            {
                routes.Add(routeDescriptor);
            }
        }

        public IEnumerable<RouteDescriptor> GetRoutes()
        {
            return new[]   
            {
                new RouteDescriptor 
                {
                            Route = new Route(
                                "api/favorites/GetArticles/{userId}",
                                new RouteValueDictionary {
                                                            {"area", "YIT.FavoriteArticles"},
                                                            {"controller", "FavoriteArticles"},
                                                            {"action", "GetUserArticles"}
                                                        },
                                new RouteValueDictionary(),
                                new RouteValueDictionary {
                                                            {"area", "YIT.FavoriteArticles"}
                                                        },
                                new MvcRouteHandler())
                },
            };
        }
    }
}




my controller is:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace YIT.FavoriteArticles.Controllers
{
    public class FavoriteArticlesController : ApiController
    {
        public FavoriteArticlesController() { }

        public HttpResponseMessage GetUserArticles(int userId)
        {


            try
            {


                return Request.CreateResponse(HttpStatusCode.OK, "1");
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, ex.Message);
            }
        }

    }
}

and I've enabled the module

can you tell if something may be missing?

 

 

 

Coordinator
Dec 5, 2012 at 6:12 PM
Edited Dec 6, 2012 at 8:19 PM

HttpResponseMessage is not an ActionResult, so GetUserArticles is not an action. Oops, I missed the part about this being an API controller, not a regular controller.

Dec 6, 2012 at 10:31 AM

 

http://orchard.codeplex.com/discussions/397191