在IIS应用程序(虚拟目录)中运行ABP Web API时出现问题

时间:2019-06-12 18:55:02

标签: iis aspnetboilerplate asp.net-boilerplate

我使用模块零核心模板(Angular和ASP.NET Core,但我使用的是.NET Full Framework 4.x)使用​​ASP.NET Boilerplate(ABP)创建了一个应用程序。当我将标准部署到IIS(Angular运行在端口80上,ASP.NET Web API运行在端口21021上)时,它运行良好。但是,如果尝试将Web API部署到IIS应用程序(类似于虚拟目录),则Swagger集成会遇到麻烦。我发现的先前答案尚未提供解决问题的方法。

例如,IIS应用程序的URL为http://example.com/myapi。导航到该URL应该重定向到http://example.com/myapi/swagger,但是它重定向到http://example.com/swagger(不存在)。在appsettings.json中,ServerRootAddress设置为http://example.com/myapi。这可能是由于Web.Host项目的HomeController中的以下代码导致的:

public IActionResult Index()
{
    return Redirect("/swagger");
}

它似乎被硬编码到网站的根目录,而不是使用相对路径,但是我不确定。如果我修改控制器以使用相对路径,则会加载Swagger页面,但无法进行身份验证。它尝试导航到http://example.com/api/TokenAuth/Authenticate而不是http://example.com/myapi/api/TokenAuth/Authenticate。同样,它不使用相对路径。这是来自abp.swagger.js的相关代码:

xhr.open('POST', '/api/TokenAuth/Authenticate', true);

这是ABP或“模块零核心”模板的限制,还是我需要在ABP,Swagger / Swashbuckle或IIS中进行一些其他配置才能使其正常工作? FWIW,该应用程序在ABP版本3.8上运行。

1 个答案:

答案 0 :(得分:0)

我最终将ABP模板代码更改为在必要时使用相对路径。

HomeController.cs:

public IActionResult Index()
{
    return Redirect("~/swagger");
}

abp.swagger.js:

xhr.open('POST', '../api/TokenAuth/Authenticate', true);
...
xhrTenancyName.open('POST', '../api/services/app/Account/IsTenantAvailable', true);

要使用SignalR,还需要进行更多更改。

在Angular应用中也需要进行一些更改。

index.html:

<base href="/myvirtualpath/">

sidebar-user-area.component.html:

<img src="assets/images/user.png" width="48" height="48" alt="User" />