要访问ASP.Net Web API控制器,需要确保以下几点:
确保Web API控制器已经正确设置和配置。
确保在Angular 4代码中正确设置和配置HTTP请求以访问Web API控制器。
下面是一个示例:
[Route("api/[controller]")]
public class MyController : Controller
{
// GET api/my
[HttpGet]
public IActionResult Get()
{
// 返回数据
}
}
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class MyService {
constructor(private http: Http) { }
getMyData() {
return this.http.get('http://localhost:5000/api/my')
.map(response => response.json());
}
}
在这个示例中,我们创建了一个名为"MyService"的Angular服务,其中的"getMyData"方法将使用Angular的HTTP模块发送一个GET请求到"http://localhost:5000/api/my"地址,并将响应转换为JSON格式。
请注意,上述示例中的URL地址可能需要根据你的实际情况进行修改。
这是一个简单的解决方法示例,具体的解决方法取决于你的实际情况。如果还有其他问题,请提供更多的详细信息,我会尽力帮助你解决。