由於各種工程需求,我需要在同一個應用程序域中開發一個新的ASP.NET Web API應用程序(命名爲BarApp)現有的應用程序(名爲FooApp)。將ASP.NET Web API應用程序配置爲ASP.NET MVC應用程序下的虛擬目錄
我想在ASP.NET 8.5上的現有ASP.NET MVC應用程序(FooApp)下將ASP.NET Web API應用程序(BarApp)配置爲虛擬目錄。雖然許多帖子都談到如何配置虛擬目錄,但它們都不起作用。
下面是配置片段
<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
在富應用程序,我配置的路由:添加則會忽略路線爲所有路徑酒吧
--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服務器配置爲不列出此目錄的內容。
無法訪問欄的網頁API:https://mywebsite.test.com:4433/Bar/api/values HTTP錯誤404.0 - 找不到 您正在尋找已被刪除的資源,有其名稱更改,或者暫時不可用。
網絡API庫2.2.1
西門,非常感謝您的探測分析。但是,我嘗試使用configure:在Bar App中,MVC路由更改爲「url:」{controller}/{action}/{id}「,」,Web API路由更改爲「routeTemplate:」 API/{控制器} /(編號) 「」。對https://mywebsite.test.com:4433/Bar/api/values的請求仍然返回404.請注意,Bar應用程序具有ValuesController來處理對api/values的獲取請求。你有任何其他想法來找出原因嗎?謝謝! –
從跟蹤日誌,我確實看到GENERAL_CHILD_REQUEST_START活動,詳情如下: SITEID:6 RequestURL:https://開頭本地主機:4433 /酒吧/ API /值 RequestVerb:GET RecursiveLevel:1 但GENERAL_CHILD_REQUEST_END事件返回404錯誤。 –
請有人給微軟打電話來解決這個問題。我現在不得不恢復到頁面方法,直到我可以得到這個工作 – ojorma