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