如何在 ASP.NET Core 1.1 中正确计算 TypeScript 中的 Web API 方法 URL。解决方案?
How to properly compute Web API methods URLs in TypeScript within ASP.NET Core 1.1. solution?
我正在尝试设置 Visual Studio 2017 ASP.NET Core 1.1 解决方案以使用 Angular2 SPA。
我使用以下命令创建了一个示例 csproj
:
npm install -g yo generator-aspnetcore-spa
yo aspnetcore-spa
然后在 Visual Studio 2017 社区版中打开它。
当 运行 使用 IIS Express 10 时,它工作正常(路由、刷新、Web API 调用)。但是,在 IIS 下的 Web 应用程序中部署时,Web API 调用因路径不包含 Web 应用程序名称而失败:
fetchdata.component.ts文件定义数据获取如下:
constructor(http: Http) {
http.get('/api/SampleData/WeatherForecasts').subscribe(result => {
this.forecasts = result.json() as WeatherForecast[];
});
}
这在 IIS Express 中工作正常,因为路径 http://localhost:port/api/SampleData/WeatherForecasts
是正确的。但是,这在 IIS 下失败,因为正确的路径是:
http://localhost:port/ApplicationName/api/SampleData/WeatherForecasts
URL 中的硬编码应用程序名称可行,但这不是可行的解决方案。
我已更改 _Layout.cshtml 以正确设置基础(路径),但它不起作用:
<!DOCTYPE html>
@{
string basePath = Url.Content("~");
}
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WebApplicationBasic</title>
<base href="@basePath" />
<link rel="stylesheet" href="~/dist/vendor.css" asp-append-version="true" />
</head>
<body>
Base path: @basePath
@RenderBody()
@RenderSection("scripts", required: false)
</body>
</html>
如果我理解正确,该路径被假定为相对于 tsconfig.json
文件,该文件位于 wwwroot 文件夹之外。这可以解释为什么从 ts 生成的 js 的所有路径都不考虑 Web 应用程序文件夹。
问题:如何为使用 TypeScript 文件的调用动态指定 URL 前缀?
您必须使用相对路径而不是绝对路径。只需从 http.get(...)
调用中删除 /
。
您已经正确设置了基本路径(通过 <base href="~/"/>
也可以更轻松地工作)。这将确保基本路径始终用作相对 URL 的起点。这也适用于 ajax 调用。所有新的浏览器都已经支持 <base href=""/>
。另见我以前的post.
我正在尝试设置 Visual Studio 2017 ASP.NET Core 1.1 解决方案以使用 Angular2 SPA。
我使用以下命令创建了一个示例 csproj
:
npm install -g yo generator-aspnetcore-spa
yo aspnetcore-spa
然后在 Visual Studio 2017 社区版中打开它。
当 运行 使用 IIS Express 10 时,它工作正常(路由、刷新、Web API 调用)。但是,在 IIS 下的 Web 应用程序中部署时,Web API 调用因路径不包含 Web 应用程序名称而失败:
fetchdata.component.ts文件定义数据获取如下:
constructor(http: Http) {
http.get('/api/SampleData/WeatherForecasts').subscribe(result => {
this.forecasts = result.json() as WeatherForecast[];
});
}
这在 IIS Express 中工作正常,因为路径 http://localhost:port/api/SampleData/WeatherForecasts
是正确的。但是,这在 IIS 下失败,因为正确的路径是:
http://localhost:port/ApplicationName/api/SampleData/WeatherForecasts
URL 中的硬编码应用程序名称可行,但这不是可行的解决方案。
我已更改 _Layout.cshtml 以正确设置基础(路径),但它不起作用:
<!DOCTYPE html>
@{
string basePath = Url.Content("~");
}
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WebApplicationBasic</title>
<base href="@basePath" />
<link rel="stylesheet" href="~/dist/vendor.css" asp-append-version="true" />
</head>
<body>
Base path: @basePath
@RenderBody()
@RenderSection("scripts", required: false)
</body>
</html>
如果我理解正确,该路径被假定为相对于 tsconfig.json
文件,该文件位于 wwwroot 文件夹之外。这可以解释为什么从 ts 生成的 js 的所有路径都不考虑 Web 应用程序文件夹。
问题:如何为使用 TypeScript 文件的调用动态指定 URL 前缀?
您必须使用相对路径而不是绝对路径。只需从 http.get(...)
调用中删除 /
。
您已经正确设置了基本路径(通过 <base href="~/"/>
也可以更轻松地工作)。这将确保基本路径始终用作相对 URL 的起点。这也适用于 ajax 调用。所有新的浏览器都已经支持 <base href=""/>
。另见我以前的post