【发布时间】: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