我有正在POST的JSON数据,它们通过完整的URL引用其他资源,如下所示:
{
jobItems : [
"http://example.com/item/3",
"http://example.com/item/5"
]
}
在我的WebAPI控制器中,我通过重用相应的路由来解析这些URL:
private static IHttpRouteData ParseLink(
this ApiController controller,
string routeName,
Uri uri)
{
var fakeRequest = new HttpRequestMessage(HttpMethod.Get, uri);
string virtualPathRoot = controller.Configuration.VirtualPathRoot;
var route = controller.Configuration.Routes[routeName];
return route.GetRouteData(virtualPathRoot, fakeRequest);
}
以上在独立的自托管WebAPI部署中可以正常工作。但是,当我在Web主机(IIS)上的ASP.NET MVC4应用程序中部署我的WebAPI控制器时,GetRouteData会引发以下异常:
System.NotImplementedException
at System.Web.HttpContextBase.get_Items()
at System.Web.Http.WebHost.Routing.HttpContextBaseExtensions.GetHttpRequestMessage(HttpContextBase context)
at System.Web.Http.WebHost.Routing.HttpWebRoute.ProcessConstraint(HttpContextBase httpContext, Object constraint, String parameterName, RouteValueDictionary values, RouteDirection routeDirection)
at System.Web.Routing.Route.ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection)
at System.Web.Routing.Route.GetRouteData(HttpContextBase httpContext)
at System.Web.Http.WebHost.Routing.HostedHttpRoute.GetRouteData(String rootVirtualPath, HttpRequestMessage request)
出了什么问题?我该如何解决这个问题?
答案 0 :(得分:0)
您可以尝试以下方法进行修复:
private static IHttpRouteData ParseLink(this ApiController controller, Uri uri)
{
var fakeRequest = new HttpRequestMessage(HttpMethod.Get, uri);
return controller.Configuration.Routes.GetRouteData(fakeRequest);
}