3

我一直在尝试WebApi使用Sitecore 7.5(我能够获得与7.2相同的代码)我在配置中留下了对的引用,MVC 5.1 当我尝试访问使用属性映射的路由时出现以下异常:

[RoutePrefix("test/api/Other")]
[Route("{action=Get}")]
public class OtherController : ApiController
{
    [HttpGet]
    public string GetId()
    {
        return "test";
    }
}

消息:“发生错误。”,ExceptionMessage:“值不能为空。参数名称:key”,ExceptionType:“System.ArgumentNullException”,StackTrace:“在 System.Collections.Generic.Dictionary 2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& 值)在 Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController(HttpRequestMessage request) 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancelToken) 在 System.Web.Http.Dispatcher .HttpControllerDispatcher.d__0.MoveNext()"

我在应用程序启动中的代码如下:

protected void Application_Start(object sender, EventArgs e)
{
    GlobalConfiguration.Configure(ConfigureRoutes);
}

public static void ConfigureRoutes(HttpConfiguration config)
{
    GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
}

任何帮助,将不胜感激....

4

1 回答 1

0

从 Sitecore 7.5 开始,他们正在用IHttpControllerSelector自己的NamespaceHttpControllerSelector不支持属性路由的默认值替换。

但是,可以解决此问题。在此之后,您必须创建自己的版本NamespaceHttpControllerSelector并将其修补到initialize管道中:

Sitecore.Services.Infrastructure.Sitecore.Pipelines.ServicesWebApiInitializer, Sitecore.Services.Infrastructure.Sitecore

我已经创建了一个 Sitecore 包和一个 NuGet 包来执行此操作,具体取决于您的喜好和需求。

“自定义”包在您的解决方案中创建代码,因此如果您有特殊需要,您可以自己编辑它。Sitecore 包和标准 NuGet 包只是将我的程序集放在 bin 文件夹中,并创建一个配置文件App_Config\Include来修补initialize管道。

如果您想查看代码,或阅读更多关于该问题的信息,请查看我的 GitHub 存储库

于 2015-09-22T20:55:11.917 回答