WebApi Route在Orchard模块中返回Not Found

时间:2015-05-19 06:45:36

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

我正在创建一个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类:

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上,主页是:

http://localhost:30321/OrchardLocal

当我去

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

我找到了Not Found页面。

任何人都能解释一下吗?

2 个答案:

答案 0 :(得分:2)

您的GET方法没有参数ID。那可能是它

答案 1 :(得分:0)

我今天早些时候通过网络API呼叫了。结果改变了为我工作的链接。

当我使用Postman Chrome扩展程序进行测试时,我从使用http://localhost:30321/Orchard.web/api/ModuleName/Get转到使用http://localhost:30321/api/ModuleName/Get