我正在使用Angular 5和.Net framework 4.7,并且在角度上我是新手。我使用VS 2017创建了该项目。有角度的 service 类如下:
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);
}
}
组件类类似于:
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
)
}
}
我已经在 api 控制器中编写了以下代码。
[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;
}
}
在调试应用程序时,它完美地击中了 service.ts 中的“ StringsService ”类。调试器还会显示api控制器的正确网址,例如-https://localhost:44345/api/Strings/Index。但是控件无法达到 api 控制器的“ 索引”类。我是 在errorHandler中收到以下错误:
“ caller = TypeError:严格模式函数或用于调用它们的参数对象上可能无法访问“ caller”,“ callee”和“ arguments”属性 “
我在浏览器控制台中遇到以下错误:
任何想法为什么会发生此错误。
谢谢
Partha
答案 0 :(得分:1)
由于控制器装饰有route属性,因此应将方法route简化为以下内容(未经测试):
[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;
}
}
现在您应该可以请求api/Strings/Index
答案 1 :(得分:0)
您的路线装饰者应该是这样的
[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;
}
}