【问题标题】:UrlHelper.Link() returns nullUrlHelper.Link() 返回 null
【发布时间】:2014-06-17 09:32:51
【问题描述】:

我知道这里有许多具有此标题的问题,但是,经过这些问题,我认为发布的任何解决方案都没有帮助。

我在 Web Api v2.1 控制器中有以下代码...

    [VersionedRoute("{id:int}", 1, Name="GetEvent")]
    [HttpGet]
    public IHttpActionResult Get(int id)
    {
        try
        {
            var helper = new UrlHelper(Request);
            var link = helper.Link("GetEvent", new { id = id });
            var result = ApiBL.V1Get(id);
            if (result == null)
            {
                return NotFound();
            }
            return Ok(result);
        }
        catch (Exception ex)
        {
            throw new ApiHttpResponseException(HttpStatusCode.BadRequest, ex);
        }
    }

在分配了link 的行之后的行上放置一个断点,我可以看到它为空。我还可以看到 Request 具有我期望看到的内容...

{Method: GET, RequestUri: 'http://localhost:8087/atlas/api/events/50', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Authorization: Basic blahblahblah
  Host: localhost:8087
  Content-Length: 31
  Content-Type: application/json
}}

helper 是非空的。

下面是VersionedRouteAttribute类的定义

public class VersionedRouteAttribute : RouteFactoryAttribute
{
    public VersionedRouteAttribute(string template, int allowedVersion)
        : base(template)
    {
        AllowedVersion = allowedVersion;
    }

    public int AllowedVersion { get; private set; }

    public override IDictionary<string, object> Constraints
    {
        get
        {
            var constraints = new HttpRouteValueDictionary();
            constraints.Add("version", new VersionConstraint(AllowedVersion));
            return constraints;
        }
    }
}

我相信,从我读过的所有内容来看,这应该可行。我根本不知道出了什么问题。我会很感激任何指针。

【问题讨论】:

  • 嗯。我不相信命名参数Name 的值会传递到基础RouteAttribute
  • 我已在原始问题中添加了 versionedRouteAttribute 类定义的详细信息,以防 确实 被证明是相关的。
  • 裤子!它以某种方式连接到VersionedRouteAttribute 类。如果我用标准的RouteAttribute 替换它,它就可以工作。我尝试修改 VersionedRouteAttribute 调用以将 Name 作为参数并在基类上显式设置,虽然我可以看到它正在设置,但没有任何区别! :-(
  • 为了清楚起见,使用上面显示的代码,如果我在 VersionedRouteAttribute.Constraints 的 getter 上放置一个断点,我可以看到 Name 已正确设置。

标签: asp.net-web-api2 asp.net-web-api-routing


【解决方案1】:

我相信您正在使用来自hereRoutingConstraintsSample,它演示了带有属性路由的版本控制...如果是,那么该示例中的VersionConstraint 有一个错误...您可以修改Match 方法其中喜欢如下:

public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
    if (routeDirection == HttpRouteDirection.UriResolution)
    {
        int version = GetVersionHeader(request) ?? DefaultVersion;

        return (version == AllowedVersion);
    }

    return true; 
}

另外,您为什么还要自己实例化 UrlHelper,因为它已经由控制器上可用的 Url 属性提供给您。

【讨论】:

  • @Kiran.You 是正确的,我的VersionedRouteAttribute 是基于您引用的示例,而我的VersionConstraint 完全不同(我被要求允许描述 API 版本使用 4 种不同方法中的任何一种),我设法在 Match 方法中重现了相同的错误!
  • 顺便说一句,@Kiran,在我的实际代码中,UrlHelper 是实例化的,因为生成链接的代码 不是 在控制器中,而是在静态工厂类中;为了演示问题,我稍微移动了代码。
猜你喜欢
  • 2019-05-27
  • 2015-06-20
  • 2017-11-03
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
相关资源
最近更新 更多