在IIS中将WebAPI添加为子应用程序/嵌套应用程序

8

重现此问题的步骤:

  1. 在IIS中创建一个新的.NET 4网站(以下称为父级网站)。 将测试图像放入此网站的文件夹中,并观察您可以在浏览器中成功请求它
  2. 在父网站下添加一个指向WebAPI 2项目的虚拟目录或应用程序
  3. 尝试使用www.path-to-parent-site.com/api/something/or/other在浏览器中访问API

我收到以下错误:

 System.Web.Routing.UrlRoutingModule does not implement IHttpHandlerFactory or IHttpHandler.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Configuration.ConfigurationErrorsException: System.Web.Routing.UrlRoutingModule does not implement IHttpHandlerFactory or IHttpHandler.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ConfigurationErrorsException: System.Web.Routing.UrlRoutingModule does not implement IHttpHandlerFactory or IHttpHandler.]
   System.Web.Configuration.HandlerFactoryCache.GetHandlerType(String type) +12328272
   System.Web.Configuration.HandlerFactoryCache..ctor(String type) +27
   System.Web.HttpApplication.GetFactory(String type) +94
   System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +375
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34209 

有没有什么办法可以让它工作?我在这个特定问题上找到的相关信息很少,甚至看到这个错误的人的解决方案更少。

注意:如果我将WebAPI 2项目作为IIS中的新网站添加,它就可以完美运行;只有当它作为子级嵌套(无论是虚拟目录还是应用程序)时才会出现此问题。

谢谢


看起来你的URL有误。 - Stefan Ossendorf
2
看起来你的 URL 错误了。例如,你的 URL 是 http://www.parentsite.com,而你的虚拟目录是 MyApi。你最终的 URL 应该是 http://www.parentsite.com/MyApi/api/<controller>。 - Stefan Ossendorf
你解决过这个问题吗?我现在也卡在同样的问题上 :( - Joe Brailsford
2个回答

1
WebApi 不能被托管在虚拟目录上,如果您想这样做,需要使路由模式动态化,并从虚拟目录加载第一部分。
var virtualDirectory = request.ApplicationPath;
routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: virtualDirectory + "/api/{controller}/{id}",
    defaults: new {
        id = RouteParameter.Optional
    }
);

1
我已经添加了第二个MapHttpRoute,其中包含子虚拟目录路径的静态routeTemplate路径,并且现在在放置在主Web应用程序或作为子应用程序时都可以工作。谢谢! - Adrian Stanisławski

0

你需要使用 parentsite.com/childvirtualdirectory/api,即使 API 托管在 childvirtualdirectory 中。

在我的情况下,我在 IIS 上设置了 parentsite/api ... 然后访问 Web API,我需要使用 http://parentsite/api/api/<<controller>>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接