如何使用angular4.x的HttpClient访问.net webapi

网上查阅了很多文章,基本写的都不是很完整,整理一下。

从.net WebAPI与Angular两个方面来介绍。

一、.net WebAPI配置

.net WebAPI方面,主要是解决跨域的问题。

1、修改Web.config文件中的system.webServer

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <!--<remove name="OPTIONSVerbHandler" />-->
      <remove name="TRACEVerbHandler" />
      <!--<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />-->
    </handlers>
    
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type,authorization,mypara,username" />
        <add name="Access-Control-Allow-Methods" value="POST,GET,DELETE,PUT,OPTIONS,HEAD" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

2、修改Global.asax.cs文件,在Global类中,添加Application_BeginRequest()方法,代码如下:

        protected void Application_BeginRequest()
        {
            var list = new List<string>(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<Hero> 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<Hero> heroes = new List<Models.Hero>(
            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<Hero[]> {
    return this.httpClient.get(this.heroesUrl)
      .toPromise()
      .catch(this.handleError);
  }

  getHero(id: number): Promise<Hero> {
    const url = `${this.heroesUrl}/${id}`;
    return this.httpClient.get(url)
      .toPromise()
      .catch(this.handleError);
  }

  update(hero: Hero): Promise<Hero> {
    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<Hero> {
    return this.httpClient.post(this.heroesUrl, JSON.stringify({name: name}), this.headers)
      .toPromise()
      .catch(this.handleError);
  }

  delete(id: number): Promise<boolean> {
    const url = `${this.heroesUrl}/${id}`;
    return this.httpClient.delete(url, this.headers).toPromise()
      .then(() => null).catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    alert('Error');
    const msg = error.message || error;
    alert(msg);
    console.error('An Error Occurred', error);
    return Promise.reject(error.message || error);
  }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值