【问题标题】:Adding WebAPI as a child/nested application in IIS在 IIS 中将 WebAPI 添加为子/嵌套应用程序
【发布时间】:2015-03-06 06:28:15
【问题描述】:

重现此问题的步骤:

  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 是parentsite.com,你的虚拟目录是 MyApi。你的最终网址是parentsite.com/MyApi/api/<controller>
  • 你解决过这个问题吗?现在卡在同一个问题上:(

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


【解决方案1】:

WebApi 不应该托管在虚拟目录上,如果您想这样做,您需要使路由模式动态化并从虚拟目录加载第一部分。

var virtualDirectory = request.ApplicationPath;
routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: virtualDirectory + "/api/{controller}/{id}",
    defaults: new {
        id = RouteParameter.Optional
    }
);

【讨论】:

  • 我添加了第二个 MapHttpRoute,其静态 routeTemplate 路径包括子虚拟目录路径,现在它在放置在两个位置时都可以工作 - 主 Web 应用程序或作为子应用程序。谢谢!
【解决方案2】:

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

在我的场景中,我在 IIS 中设置了 parentsite/api...然后要访问 Web API,我需要使用 http://parentsite/api/api/&lt;&lt;controller&gt;&gt;

【讨论】:

    猜你喜欢
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多