Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi Route returns Not Found in Orchard Module

I am creating an Orchard module where i want to add a WebApi controller.

My Module.txt:

Name: ModuleName
AntiForgery: enabled
Author: The Orchard Team
Website: http://orchardproject.net
Version: 1.0
OrchardVersion: 1.0
Description: Description for the module
Features:
    ModuleName:
        Description: Description for feature ModuleName.

I have added an ApiRoutes class:

using Orchard.Mvc.Routes;
using Orchard.WebApi.Routes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace ModuleName
{
    public class ModuleNameApiRoutes : IHttpRouteProvider
    {

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

        public IEnumerable<RouteDescriptor> GetRoutes()
        {
            return new[] {
                new HttpRouteDescriptor {
                    Name = "ModuleName",
                    Priority = 5,
                    RouteTemplate = "api/modulename/{controller}/{id}",
                    Defaults = new {
                        area = "ModuleName",
                        id = RouteParameter.Optional
                    }
                }
            };
        }
    }
}

Then i have added an apicontroller:

using Newtonsoft.Json.Linq;
using Orchard;
using Orchard.Data;
using ModuleName.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ModuleName.Controllers
{
    public class ConsumptionController : ApiController
    {
        public IOrchardServices Services { get; private set; }
        private readonly IRepository<Vessel_ConsumptionPartRecord> _repository;
        public ConsumptionController(IOrchardServices orchardServices,IRepository<Vessel_ConsumptionPartRecord> repository)
        {
            _repository = repository;
        }

        // GET: Home
       public HttpResponseMessage Get()
        {

            ...
        }


    }
}

I am on Localhost and the home url is:

http://localhost:30321/OrchardLocal

When i go to

http://localhost:30321/OrchardLocal/api/ModuleName/Consumption

I get a Not Found page.

Can anyone shed some light?

like image 402
Giannis Paraskevopoulos Avatar asked Nov 01 '22 03:11

Giannis Paraskevopoulos


1 Answers

Your GET method does not have a parameter id. That might be it

like image 75
ErMasca Avatar answered Nov 15 '22 04:11

ErMasca