将ASP.NET Web API应用程序配置为ASP.NET MVC应用程序下的虚拟目录 [英] Configure ASP.NET Web API application as a Virtual Directory under ASP.NET MVC application

查看:368
本文介绍了将ASP.NET Web API应用程序配置为ASP.NET MVC应用程序下的虚拟目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Due to various engineering requirements, I need to develop a new ASP.NET Web API application (named as BarApp) within the same Application domain of an existing application (named as FooApp).

我想将ASP.NET Web API应用程序(BarApp)配置为IIS 8.5上现有ASP.NET MVC应用程序(FooApp)下的虚拟目录.尽管许多文章都谈到了如何配置虚拟目录,但是这些都无济于事.

I would like to configure an ASP.NET Web API application (BarApp) as a virtual directory under an existing ASP.NET MVC application (FooApp) on IIS 8.5. Though many many posts talked about how to configure a virtual directory, none of them work.

这是配置代码段

        <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为所有路径添加一个ingore路由

In the Foo App, I configured the routing: add an ingore route for all path with 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,保留自动生成的一个.

--WebApiConfig, keep the automatically generated one.

 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中,路由配置被修改为:

In the BarApp, the routing configure is modified as:

--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服务器配置为不列出此目录的内容.

However, the above configuration does not work, and the ignore route seems not taken into effect.
Failed to access page of Bar: https://mywebsite.test.com:4433/Bar/ HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

无法访问Bar的Web API: https://mywebsite.test.com: 4433/Bar/api/values HTTP错误404.0-找不到 您要查找的资源已被删除,名称已更改或暂时不可用.

Failed to access Web API of Bar: https://mywebsite.test.com:4433/Bar/api/values HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Web API库是2.2.1

the Web API library is 2.2.1

推荐答案

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

I think the problem with your approach is trying to host an Asp.Net application inside a Virtual Directory. Rather, you must add a child Application:

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

Then modify the web.config of the root application to avoid configuration collisions:

参考:在子应用程序中禁用继承这样,您将能够访问您的子Web API,而无需修改任何C#代码:

This way, you will be able to access your child web api without modifying any c# code:

这篇关于将ASP.NET Web API应用程序配置为ASP.NET MVC应用程序下的虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆