gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:01:32 27 4
gpt4 key购买 nike

由于各种工程要求,我需要在现有应用程序(名为 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com