在 Orchard 模块中找不到 WebApi 路由 returns

WebApi Route returns Not Found in Orchard Module

我正在创建一个 Orchard 模块,我想在其中添加一个 WebApi 控制器。

我的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.

我添加了一个 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
                    }
                }
            };
        }
    }
}

然后我添加了一个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()
        {

            ...
        }


    }
}

我在 Localhost 上,家 url 是:

http://localhost:30321/OrchardLocal

当我去

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

我得到一个“未找到”页面。

任何人都可以解释一下吗?

您的 GET 方法没有参数 ID。可能是这样

我今天早些时候通过网络 api 电话收到了这个。原来改变 link 对我有用。

当我使用 Postman Chrome 扩展进行测试时,我不再使用 http://localhost:30321/Orchard.web/api/ModuleName/Get to using http://localhost:30321/api/ModuleName/Get