网上查阅了很多文章,基本写的都不是很完整,整理一下。
从.net WebAPI与Angular两个方面来介绍。
一、.net WebAPI配置
.net WebAPI方面,主要是解决跨域的问题。
1、修改Web.config文件中的system.webServer
2、修改Global.asax.cs文件,在Global类中,添加Application_BeginRequest()方法,代码如下:
protected void Application_BeginRequest()
{
var list = new List(Request.Headers.AllKeys);
if (list.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.End();
}
}
3、附上对应官方教程《Tour of Hero》的WebAPI代码,(仅做测试调用,修改等操作不会生效):
namespace ApiForAngular.Models
{
public class Hero
{
public int id { get; set; }
public string name { get; set; }
}
}
namespace ApiForAngular.Controllers
{
[System.Web.Mvc.Route("api/[Controller]")]
public class HeroController : ApiController
{
[HttpGet]
public List Get()
{
return heroes;
}
[HttpGet]
public Hero Get(int id)
{
foreach (var item in heroes)
{
if (item.id == id)
return item;
}
return null;
}
[HttpPost]
public Hero Post([FromBody]Hero hero)
{
hero.id = curId;
curId++;
heroes.Add(hero);
return hero;
}
[HttpPut]
public Hero Put(int id, [FromBody]Hero value)
{
for (int i = 0; i < heroes.Count; i++)
{
if (id == heroes[i].id)
{
heroes[i].name = value;
return heroes[i];
}
}
return null;
}
[HttpDelete]
public void Delete(int id)
{
heroes.Remove(heroes.Find(hero => hero.id == id));
}
protected List heroes = new List(
new Hero[] {
new Hero() { id = 1, name = "Tom" },
new Hero() { id = 2, name = "Tim" },
new Hero() { id = 3, name = "Jane" },
new Hero() { id = 4, name = "Jack Ma" },
new Hero() { id = 5, name = "Machael" },
new Hero() { id = 6, name = "Mr DJ" },
new Hero() { id = 7, name = "Bill Gates" },
new Hero() { id = 8, name = "Steven Jobs" },
new Hero() { id = 9, name = "Paul Graham" }
});
protected int curId = 10;
}
}
二、Angular调用方法
首先,需要注意的是,如果是使用了《Tour of Hero》的教程代码,需要去掉所有与内存WebAPI相关的引用,即:去掉与InMemoryWebApiModule相关的全部内容。
我的hero.service.ts文件如下:(其中,heroesURL配置成自己发布的WebAPI的地址与名称即可)
import { Injectable } from '@angular/core';
import {Hero} from '../Models/hero';
import 'rxjs/add/operator/toPromise';
import {HttpClient, HttpHeaders} from '@angular/common/http';
@Injectable()
export class HeroService {
private heroesUrl = 'http://**Address of Ip or WebSite**/**Application Name**/api/**Name of API**';
private headers = { headers: new HttpHeaders({'Content-Type': 'application/json'}) };
constructor(private httpClient: HttpClient) { }
getHeroes(): Promise {
return this.httpClient.get(this.heroesUrl)
.toPromise()
.catch(this.handleError);
}
getHero(id: number): Promise {
const url = `${this.heroesUrl}/${id}`;
return this.httpClient.get(url)
.toPromise()
.catch(this.handleError);
}
update(hero: Hero): Promise {
const url = `${this.heroesUrl}/${hero.id}`;
return this.httpClient
.put(url, JSON.stringify(hero), this.headers)
.toPromise()
.then(() => hero)
.catch(this.handleError);
}
create(name: string): Promise {
return this.httpClient.post(this.heroesUrl, JSON.stringify({name: name}), this.headers)
.toPromise()
.catch(this.handleError);
}
delete(id: number): Promise {
const url = `${this.heroesUrl}/${id}`;
return this.httpClient.delete(url, this.headers).toPromise()
.then(() => null).catch(this.handleError);
}
private handleError(error: any): Promise {
alert('Error');
const msg = error.message || error;
alert(msg);
console.error('An Error Occurred', error);
return Promise.reject(error.message || error);
}
}