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

编程入门 行业动态 更新时间:2024-10-28 12:25:58
本文介绍了将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页面: mywebsite.test: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: mywebsite.test:4433/Bar/ HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

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

Failed to access Web API of Bar: mywebsite.test: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:

参考:在子应用程序中禁用继承和 validateIntegratedModeConfiguration与InheritInChildApplications发生冲突.

这样,您将能够访问您的子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应用程序下的虚拟目录

本文发布于:2023-11-04 05:58:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   虚拟目录   NET   ASP   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!