Xia
Xia

Reputation: 169

Controllers.cs works on diffrent address - .net core angular app from visual studio template

I created a new project in .Net Core and Agnular from visual studio template and I cant get data from controller. It works with controller genereted from template WeatherController.cs but not with my own. I mean it works when I call controller like that:

https://localhost:5001/home/getData/1

but application starts at 5002 and method from weatherController also works with 5002. I don't understand why one controller work on 5002 and another on 5001. Could anyone can explain me how to configure this propertly or send me some source to understand how this is works. Thanks

Configuration:

    {
  "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"
      }
    }
  }
}

enter image description here

Controller:

[Route("[controller]")]
[ApiController]
public class HomeController : ControllerBase
{

    [HttpGet]
    [Route("GetData/{id}")]
    public ActionResult<string> GetData(int id)
    {
        return "ok";
    }

enter image description here

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));

1st one works only when I change 5002 to 5001 but second one works on 5002...

https://localhost:5001/home/getData/1 - OK

https://localhost:5002/home/getData/1 - NOT OK

https://localhost:5002/weatherforecast - OK

Upvotes: 5

Views: 477

Answers (1)

FireAlkazar
FireAlkazar

Reputation: 1882

Try to include /home path to ClientApp\proxy.conf.js file

const PROXY_CONFIG = [
  {
    context: [
      "/weatherforecast",
      "/home"
   ],
    target: "https://localhost:5001",
    secure: false
  }
]

module.exports = PROXY_CONFIG;

This is angular config for proxy

Upvotes: 5

Related Questions