ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-WebApi与Unity注入

系列目录

前言:

有时候咱们系统须要开放数据给手机App端或其余移动设备,不得不说Asp.net WebApi是目前首选html

本节记录Asp.net MVC WebApi怎么利用Unity注入。系列开头已经讲解了普通的Asp.net MVC如何用Unity注入容器jquery

不明白什么是IOC,DI,控制反转的自行百度补脑,不然没法阅读本文数据库

其实这也是一次技术上的记录,由于找遍大百度竟然没有能够用的利用Unity注入的WebApi!感谢随风朋友的提醒。才能完成本节的指导网络

为了更好的理解,请下载代码工具

连接:https://pan.baidu.com/s/1QjKHPbyNELrrZhuM3JD0dA 密码:lv1f开发工具

(这是一个已经包含了Unity注入的普通MVC例子)包含4个须要被注入的类库,BLL,IBLL,DAL,IDAL测试

开发工具:VS2013+SQL2012(数据库不是必要)

开始:

1.新建Asp.Net MVC WebApi项目

2.安装Unity.WebApi程序包解析一下,这里有依赖项:Unity >=4.0.1

(咱们以前的注入就只安装了Unity)因此Unity.WebApi是须要Unity4.0.1支持的。spa

安装Unity.WebApi会自动安装不少依赖项:Unity (≥ 4.0.1),CommonServiceLocator,Microsoft.AspNet.WebApi.Core,Microsoft.AspNet.WebApi.Client ,Newtonsoft.Json .net

网络很差会很漫长,须要难心等待。或者到nuget官方下载离线的调试

3.Apps.Core也须要一样安装Untiy.WebApi

安装完成后在Apps.WebApi下将自动多出一个文件

添加代码到UnityConfig.cs

using Apps.Core;
using Microsoft.Practices.Unity;
using System.Web.Http;
using Unity.WebApi;

namespace Apps.WebApi
{
    public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            UsingUnityContainer.Init();
            DependencyRegisterType.Container_Sys(ref UsingUnityContainer._container);
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(UsingUnityContainer._container);
           
        }
    }
}

最后添加UnityConfig.RegisterComponents();到Global.asax 。必须在全局文件添加,本注入是运行时注入

using Apps.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace Apps.WebApi
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            UnityConfig.RegisterComponents();
        }
    }
}

4.修复错误

运行以后会出现错误

这是因为Web.config节点缺少解析形成。

须要添加如下节点

 <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>

运行以后就没有问题了。

在Home/Index.cshtml编写一个简单的请求的测试一下

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(function(){
    $.get("/Api/Values/5",function(data){
         alert(data);
        });
    });
</script>

5.运行并调试

添加控制反转代码到ValuesController

  [Dependency]
   public ISysPersonBLL m_BLL { get; set; }

这样咱们就能够用m_BLL来访问BLL的方法,达到控制反转的目的

若是断点执行到调用处,那么恭喜你,注入顺利成功,能够调用了(配图为返回数据库第一条数据的ID,数据和脚本在源码下载查看)

能够顺利访问到数据库

相关文章
相关标签/搜索