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

标签 asp.net-mvc asp.net-web-api virtual-directory

由于各种工程要求,我需要在现有应用程序(名为 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 App 中,我配置了路由:为所有带有 Bar 的路径添加一个 inore 路由
--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/values
HTTP 错误 404.0 - 未找到
您要查找的资源已被删除、更名或暂时不可用。

Web API 库是 2.2.1

最佳答案

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

IIS - Add child application

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

Avoid collisions in child's web.config

引用:Disable inheritance in child applicationsvalidateIntegratedModeConfiguration and inheritInChildApplications clash .

这样,您就可以在不修改任何 C# 代码的情况下访问您的子 Web api:

Child web api

关于asp.net-mvc - 将 ASP.NET Web API 应用程序配置为 ASP.NET MVC 应用程序下的虚拟目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22109026/

相关文章:

asp.net-mvc - 获取完整的请求 url,包括 Controller 中的参数

.net - WebApi 传输字节数组为空

azure - 将虚拟目录添加到现有网站

asp.net - 为什么 JavaScript 无法在现有虚拟目录下的我的网站上运行?

asp.net - 在同一域上的 IIS 应用程序之间共享身份验证

asp.net-mvc - 将原始 html 从 View 传递到 Controller

asp.net-mvc - ASP.NET MVC 编辑多个子记录的示例

c# - 在 web api 和 odata 中处理大型数据集

c# - 带有 Swagger 和 FluentValidation 的 Web API 2 文档

asp.net-mvc - 使用 .Net MVC : How to display the label when selected but save the value 的 JQuery UI 自动完成