sitecore - Sitecore 7.5 的 MVC 检测和 WebApi 属性路由

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

我一直在尝试让 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";
    }
}

Message: "An error has occurred.", ExceptionMessage: "Value cannot be null. Parameter name: key", ExceptionType: "System.ArgumentNullException", StackTrace: " at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value) at Sitecore.Services.Infrastructure.Web.Http.Dispatcher.NamespaceHttpControllerSelector.SelectController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at 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 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 .

关于sitecore - Sitecore 7.5 的 MVC 检测和 WebApi 属性路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150618/

相关文章:

Sitecore WFFM : Issues Submitting form programmatically

Sitecore media src路径错误?

indexing - 将 sitecore 6.6 索引配置升级到 sitecore 7(使用 ComputedFields)

Sitecore 多个自定义用户配置文件

sitecore - 按显示名称对 treelistex 进行排序,每种语言的可用项目和选定项目

sitecore - Sitecore Search ITagRepository 实现细节

asp.net-mvc - 尝试创建类型为 'TypeNewsController' 的 Controller 时出错

C# ApiController 返回列表但 postman 显示 "Could not get any response"

c# - 单元测试中 Web Api Controller 的 ContentResult 始终返回 null