c# - WebApi 路由返回 Not Found in Orchard Module

标签 c# orchardcms asp.net-web-api

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

我的模块.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 类:

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。可能是这样

关于c# - WebApi 路由返回 Not Found in Orchard Module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30318514/

相关文章:

c# - 有没有办法可以异步运行 Database.SqlQuery?

c# - 如何在我的 Web Api 方法中处理 HttpResponseMessage?

c# - Silverlight 3 和动画 gif

c# - 有什么理由不使用标准 resx+static 绑定(bind)来本地化 WPF 吗?

orchardcms - Orchard 的 CMS 中每个生命周期事件的定义是什么?

c# - 如何最好地链接到 API Controller

asp.net - 具有复杂对象的MVC WebApi HttpGet

c# - 使用 [Obsolete] 属性的弃用行为

c# - Orchard Shape 类型 ResizeMediaUrl 未找到异常,MediaLibrary 组件不工作

c#-4.0 - 是否可以使用自定义类型属性的内容创建 Orchard 自动路线?