Controllers.cs 适用于不同地址 - 来自 visual studio 模板的 .net 核心 angular 应用程序
Controllers.cs works on diffrent address - .net core angular app from visual studio template
我从 visual studio 模板在 .Net Core 和 Agnular 中创建了一个新项目,但我无法从控制器获取数据。它适用于从模板 WeatherController.cs 生成的控制器,但不适用于我自己的控制器。我的意思是当我这样调用控制器时它会起作用:
https://localhost:5001/home/getData/1
但应用程序从 5002 开始,weatherController 中的方法也适用于 5002。我不明白为什么一个控制器在 5002 上工作而另一个在 5001 上工作。
任何人都可以向我解释如何正确配置它或向我发送一些资源以了解它是如何工作的。谢谢
配置:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49323",
"sslPort": 44374
}
},
"profiles": {
"Server": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "https://localhost:5002",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
}
}
}
控制器:
[Route("[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
[HttpGet]
[Route("GetData/{id}")]
public ActionResult<string> GetData(int id)
{
return "ok";
}
sevice.ts
//baseURL - https://localhost:5002
return this.http.get<any>(this.baseURL + 'home/GetData/' + id)
.pipe(
retry(1),
catchError(this.errorHandler)
);
http.get<WeatherForecast[]>(baseUrl + 'weatherforecast').subscribe(result => {
this.forecasts = result;
}, error => console.error(error));
第一个仅在我将 5002 更改为 5001 时有效,但第二个在 5002 上有效...
https://localhost:5001/home/getData/1 - 好的
https://localhost:5002/home/getData/1 - 不正常
https://localhost:5002/weatherforecast - 好的
尝试将 /home
路径包含到 ClientApp\proxy.conf.js
文件
const PROXY_CONFIG = [
{
context: [
"/weatherforecast",
"/home"
],
target: "https://localhost:5001",
secure: false
}
]
module.exports = PROXY_CONFIG;
我从 visual studio 模板在 .Net Core 和 Agnular 中创建了一个新项目,但我无法从控制器获取数据。它适用于从模板 WeatherController.cs 生成的控制器,但不适用于我自己的控制器。我的意思是当我这样调用控制器时它会起作用:
https://localhost:5001/home/getData/1
但应用程序从 5002 开始,weatherController 中的方法也适用于 5002。我不明白为什么一个控制器在 5002 上工作而另一个在 5001 上工作。 任何人都可以向我解释如何正确配置它或向我发送一些资源以了解它是如何工作的。谢谢
配置:
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49323",
"sslPort": 44374
}
},
"profiles": {
"Server": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "https://localhost:5002",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
}
}
}
}
控制器:
[Route("[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
[HttpGet]
[Route("GetData/{id}")]
public ActionResult<string> GetData(int id)
{
return "ok";
}
sevice.ts
//baseURL - https://localhost:5002
return this.http.get<any>(this.baseURL + 'home/GetData/' + id)
.pipe(
retry(1),
catchError(this.errorHandler)
);
http.get<WeatherForecast[]>(baseUrl + 'weatherforecast').subscribe(result => {
this.forecasts = result;
}, error => console.error(error));
第一个仅在我将 5002 更改为 5001 时有效,但第二个在 5002 上有效...
https://localhost:5001/home/getData/1 - 好的
https://localhost:5002/home/getData/1 - 不正常
https://localhost:5002/weatherforecast - 好的
尝试将 /home
路径包含到 ClientApp\proxy.conf.js
文件
const PROXY_CONFIG = [
{
context: [
"/weatherforecast",
"/home"
],
target: "https://localhost:5001",
secure: false
}
]
module.exports = PROXY_CONFIG;