【问题标题】:SDammann.WebApi.Versioning : asp.net versioningSDammann.WebApi.Versioning : asp.net 版本控制
【发布时间】:2016-06-24 22:11:25
【问题描述】:
config.Routes.MapHttpRoute(
    name: "Route1",
    routeTemplate: "api/v{version}/Participant/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

config.Routes.MapHttpRoute(
    name: "Route2",
    routeTemplate: "api/v{version}/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional },
    constraints: null,
    handler : new TokenValidationHandler() 
);

我的控制器是ParticipantController

如果我添加两个路由,Sdamman dll 会抛出异常(对象空引用错误)。我需要两条路线,只为“Route2”添加MessageHandler

谁能帮我解决这个问题?

我在global.asax.cs 文件中添加了以下行

GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector),
                    new RouteVersionedControllerSelector(GlobalConfiguration.Configuration));

由于我使用的是 .Net 框架版本 4,因此我使用了包 id="SDammann.WebApi.Versioning" version="2.0.0.3" targetFramework="net40"

异常详情

“System.NullReferenceException”类型的异常发生在 SDammann.WebApi.Versioning.dll 但未在用户代码中处理

附加信息:对象引用未设置为 对象。

在 SDammann.WebApi.Versioning.VersionedControllerSelector.GetControllerNameFromRequest(HttpRequestMessage 请求)在 z:\Developing\Visual Studio\Projects\SDammann.WebApi.Versioning\src\SDammann.WebApi.Versioning\VersionedControllerSelector.cs:line 187 在 SDammann.WebApi.Versioning.RouteVersionedControllerSelector.GetControllerIdentificationFromRequest(HttpRequestMessage 请求)在 z:\Developing\Visual Studio\Projects\SDammann.WebApi.Versioning\src\SDammann.WebApi.Versioning\RouteVersionedControllerSelector.cs:line 34 在 SDammann.WebApi.Versioning.VersionedControllerSelector.SelectController(HttpRequestMessage 请求)在 z:\Developing\Visual Studio\Projects\SDammann.WebApi.Versioning\src\SDammann.WebApi.Versioning\VersionedControllerSelector.cs:line 112 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage 请求,CancellationToken 取消令牌)在 System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage 请求,CancellationToken 取消令牌)

【问题讨论】:

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


    【解决方案1】:

    我发现了问题所在。我错过了在 Route1 中添加默认控制器。

    工作代码:

    config.Routes.MapHttpRoute(
        name: "Route1",
        routeTemplate: "api/v{version}/Participant/{action}/{id}",
        defaults: new { controller: "Participant", id = RouteParameter.Optional }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 2016-06-15
      • 2010-11-03
      • 2015-10-28
      • 1970-01-01
      • 2021-06-19
      • 2010-10-01
      相关资源
      最近更新 更多