I am trying to set up a Visual Studio 2017 ASP.NET Core 1.1 solution to work with an Angular2 SPA.
I have created a sample csproj using the following commands:
npm install -g yo generator-aspnetcore-spa
yo aspnetcore-spa
and then opened it in Visual Studio 2017 Community Edition.
When running using IIS Express 10, it works fine (routing, refresh, Web API calls). However, when deploying within a Web Application under IIS, Web API calls fails due to path not containing Web application name:
fetchdata.component.ts file defines data fetch as follows:
constructor(http: Http) {
http.get('/api/SampleData/WeatherForecasts').subscribe(result => {
this.forecasts = result.json() as WeatherForecast[];
});
}
This works fine in IIS Express, as path http://localhost:port/api/SampleData/WeatherForecasts is correct. However, this fails under IIS because the correct path is:
http://localhost:port/ApplicationName/api/SampleData/WeatherForecasts
Hardcoding application name in URL works, but this is not a viable solution.
I have changed _Layout.cshtml to properly set base (path), but it does not work:
<!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>
If I understood correctly, the path is assumed to be relative to tsconfig.json file which is placed outside wwwroot folder. That would explain why all path from js generated from ts do not take web application folder into account.
Question: How can I dynamically specify URL prefix for calls with TypeScript files?