【问题标题】:MVC instrumentation & WebApi attribute routing for Sitecore 7.5Sitecore 7.5 的 MVC 检测和 WebApi 属性路由
【发布时间】:2015-01-24 20:49:20
【问题描述】:

我一直在尝试让 WebApiSitecore 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”,异常类型: “System.ArgumentNullException”,堆栈跟踪:“在 System.Collections.Generic.Dictionary2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& 值)在 Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController(HttpRequestMessage 请求)在 System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage 请求,CancellationToken 取消令牌)在 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());
}

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

【问题讨论】:

  • 您是否在 Sitecore 的 IgnoreUrlPrefixes 设置中添加了路由前缀(本例中为“test/api”)?
  • 是的,我刚刚尝试将路由前缀添加到 IngnoreUrlPrefix,但它仍然无法正常工作......有趣的是相同的代码在干净的 7.2 实例中工作,我认为新的 DI Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController 出现对象引用错误
  • 我认为问题出在属性路由中,获取和对象引用在哪里,使其工作的唯一方法不是使用属性路由,而是使用经典路由,我正在谈论它帖子:sitecorecommerce.wordpress.com/2014/11/30/…
  • 我也有同样的问题。从 Sitecore 7.2 升级到 7.5,通过 web api 请求时出现相同的错误。
  • 你们知道这是否在最新的 7.5 版本中修复了吗?

标签: sitecore sitecore7 asp.net-apicontroller sitecore-mvc sitecore7.5


【解决方案1】:

从 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 管道。

如果您想查看代码,或阅读更多有关该问题的信息,请查看my GitHub repository

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多