Partha
Partha

Reputation: 479

Angular 5: Failed to load resource: the server responded with a status of 404

I am using Angular 5 and .Net framework 4.7 and I am new in angular. I have created the project using VS 2017. The angular service class is like:

import { Injectable, Inject } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import { Router } from '@angular/router';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

@Injectable()
export class StringsService {
  myAppUrl: string = "";

  constructor(private _http: Http, @Inject('BASE_URL') baseUrl: string) {
    this.myAppUrl = baseUrl;
  }

  getStrings() {
    debugger;
    console.log(this.myAppUrl);
    return this._http.get(this.myAppUrl + 'api/Strings/Index')
      .map((response: Response) => response.json())
      .catch(this.errorHandler);
  }
}

The component class is like:

import { Component, Inject  } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Router, ActivatedRoute } from '@angular/router';
import { StringsService } from '../../services/strings.service';

@Component({
  selector: 'app-strings',
  templateUrl: './strings.component.html'
})

export class StringsComponent {
  public stringsList: StringData[];

  constructor(public http: Http, private _router: Router, private _stringsService: StringsService) {
    this.getStrings();
  }

  getStrings() {
    debugger;
    this._stringsService.getStrings().subscribe(
      data => this.stringsList = data
    )
  }
}

I have written the following code in api controller.

[Route("api/[controller]")]
    [ApiController]
    public class StringsController : Controller
    {
        SSDMContext db = new SSDMContext();

        // GET: api/student
        [HttpGet]
        [Route("api/Strings/Index")]
        public IEnumerable<Strings> Index()
        {
            return db.Strings;
        }
    }

When I am debugging my application it is hitting the "StringsService" class in service.ts perfectly. The debugger also showing the right url of the api controller like - https://localhost:44345/api/Strings/Index. But the control is unable to reach the "index" class of api controller. I am getting the following error in errorHandler:

"caller = TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them"

I am getting the following error in browser console:

enter image description here

Any idea why this error is happening.

Thanks

Partha

Upvotes: 1

Views: 2420

Answers (2)

Mark
Mark

Reputation: 3271

Since the controller is decorated with a route attribute, you should simplify the method route to something like this (untested):

[Route("api/[controller]")]
[ApiController]
public class StringsController : Controller
{
    SSDMContext db = new SSDMContext();

    // GET: api/student
    [HttpGet]
    [Route("Index")]
    public IEnumerable<Strings> Index()
    {
        return db.Strings;
    }
}

Now you should be able to request api/Strings/Index

Upvotes: 1

Max
Max

Reputation: 862

You Route decorators should be like this

[Route("api/Strings")]
[ApiController]
public class StringsController : Controller
{
    SSDMContext db = new SSDMContext();
    // GET: api/student
    [HttpGet]
    [Route("Index")]
    public IEnumerable<Strings> Index()
    {
        return db.Strings;
    }
}

Upvotes: 0

Related Questions