Configuring an ASP.NET Web API Application as a Virtual Directory in an ASP.NET MVC Application

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

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

Below is a snippet of configuration

        <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>

Here is the site structure:

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

In a Foo application, I configured routing: add an access route for the entire path using 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, save the automatically generated file.

 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 }
            );
          }
     }
}

In BarApp, the routing configuration changes 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 - - , .

- API: https://mywebsite.test.com:4433/Bar/api/values HTTP 404.0 - , , , .

-API 2.2.1

+4
3

( IIS ), , , , , URL- . , FooApp DNS- fooapp (http://fooapp/).

Bar fooapp, - Bar http://fooapp/Bar. (MVC WebApi):

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

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

Bar "" . WebApi (, localhost), http://localhost/Bar. http://fooapp/Bar, , http://fooapp/Bar/Bar (so http://fooapp/Bar/Bar/api/values ..).

, FooApp , /Bar, IIS, , , IIS ( ..). (https://mywebsite.test.com:4433/Bar/). , Bar, /. "" /Bar/{tokens}, ( ) 403 - .

(https://mywebsite.test.com:4433/Bar/api/values), Bar, /api/values, (, /Bar/api/values, /api/values). , , 404.

+3
+3

I had the same problem. The solution is simple: MVC RouteConfigurationyou need routes to the child virtual application (here, if you have a panel under foo, foo needs to know the bar routes).

For the bar app, you need the routes documented by Simon!

Yours faithfully

Tara

0
source

Source: https://habr.com/ru/post/1529509/


All Articles