将 ASP.NET Web API 应用程序配置为 ASP.NET MVC 应用程序下的虚拟目录

2023-11-27

由于各种工程需求,我需要在现有应用程序(名为 FooApp)的同一应用程序域内开发一个新的 ASP.NET Web API 应用程序(名为 BarApp)。

我想将 ASP.NET Web API 应用程序 (BarApp) 配置为 IIS 8.5 上现有 ASP.NET MVC 应用程序 (FooApp) 下的虚拟目录。尽管很多帖子都讨论了如何配置虚拟目录,但它们都不起作用。

这是配置片段

        <site name="FooApp" id="3" serverAutoStart="true">
            <application path="/" applicationPool="FooApp">
                <virtualDirectory path="/" physicalPath="E:\FooApp" />
                <virtualDirectory path="/Bar" physicalPath="E:\BarApp" />
            </application>
            <bindings>
                <binding protocol="https" bindingInformation="*:34566:" sslFlags="0" />
            </bindings>
        </site>

这是网站结构:

-FooApp
|
|--Bin
|--View
|--Bar (as a virtual directory)
    |
    |--Bin
    |--View   

在 Foo 应用程序中,我配置路由:为带有 Bar 的所有路径添加忽略路由

--RouteConfig.cs
  namespace FooApp
  {
       public class RouteConfig
       {
          public static void RegisterRoutes(RouteCollection routes)
         {
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
             routes.IgnoreRoute("{*path}", new { path = @"Bar\/(.*)" });

             routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
             );
         }
   }
}

--WebApiConfig,保留自动生成的。

 namespace FooApp
 {
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
          }
     }
}

在BarApp中,路由配置修改为:

--RouteConfig.cs
namespace BarApp
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               name: "Default",
               url: "Bar/{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
         }
     }
}

--WebApiConfig
namespace BarApp
{
   public static class WebApiConfig
   {
       public static void Register(HttpConfiguration config)
      {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "Bar/api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
      }
   }
}

然而上面的配置并没有起作用,忽略路由似乎没有生效。
访问Bar页面失败:https://mywebsite.test.com:4433/Bar/HTTP 错误 403.14 - 禁止 Web 服务器配置为不列出该目录的内容。

访问Bar的Web API失败:https://mywebsite.test.com:4433/Bar/api/valuesHTTP 错误 404.0 - 未找到 您正在查找的资源已被删除、更名或暂时不可用。

Web API 库是 2.2.1


我认为您的方法的问题是尝试在虚拟目录中托管 Asp.Net 应用程序。相反,您必须添加一个孩子应用:

IIS - Add child application

然后修改根应用程序的web.config以避免配置冲突:

Avoid collisions in child's web.config

参考:禁用子应用程序中的继承 and validateIntegratedModeConfiguration 和继承InChildApplications 冲突.

这样,您将能够访问您的子 Web api,而无需修改任何 C# 代码:

Child web api

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 ASP.NET Web API 应用程序配置为 ASP.NET MVC 应用程序下的虚拟目录 的相关文章

随机推荐