WebApi+Spring.Net环境搭建

1.分别新建MainWeb两个web工程

  空工程并勾选上MvcWebApi

   Main做为真正实现功能的工程

   Web做为所有工程的入口

  当然不只有一个Main工程,也会有其它工程

  来共用一个Web做为入口

2.删除Main工程里的App_DataApp_StartGlobal.asax

   packages.configWeb.config(因为这些文件都依赖Web工程的)

  两个工程同时删除View文件夹,因为是WebApi所以没有View

3.Web参照引用Main工程

4.通过NuGet分别为两个工程添加Spring.Net(一定要2.0以上版本)

  安装一个Spring.Data.NHibernate4就把大多数库都导进来了

  另外再安装一个Spring.Web.Mvc5

5.修改Web工程的Global基类为SpringMvcApplication并添加下面代码:

protectedvoid Application_Start()
{
 GlobalConfiguration.Configure(WebApiConfig.Register);
}
protectedoverrideSystem.Web.Http.Dependencies.IDependencyResolverBuildWebApiDependencyResolver()
{
 varresolver =base.BuildWebApiDependencyResolver();
 varspringResolver = resolverasSpringWebApiDependencyResolver;
 if(springResolver !=null)
 {
   //这里需要导入所有工程与spring相关的配置文件
     //习惯把配置文件设置成埋入状态,因此是assembly://开关
   springResolver.AddChildApplicationContextConfigurationLocation("assembly://Main/Main.Config/Index.xml");
 }
 returnresolver;
}

6.Web工程的Web.config里添加:

<configuration>
 //必须贴着configuration来写
 <configSections>
   <sectionGroupname="spring">
     <sectionname="context"type="Spring.Context.Support.MvcContextHandler,Spring.Web.Mvc5"/>
   </sectionGroup>
 </configSections>
 <spring>
   <context>这里不能写东西!</context>
 </spring>

7.Main工程里创建Config文件夹,里面添加3.xml文件并右键属性设置成埋入:

 Controller.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectsxmlns="http://www.springframework.net">
 //type属性要根据Controller类的namespace来写,Service同样
 <objectid="LoginController"type="Main.Login.LoginController,Main"singleton="false">
   <propertyname="Service"ref="LoginService"></property>
 </object>
</objects>

 Service.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectsxmlns="http://www.springframework.net">
 <objectid="LoginService"type="Main.Login.Service.LoginService,Main"singleton="true"/>
</objects>

 Index.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectsxmlns="http://www.springframework.net">
 <importresource="Service.xml"/>
 <importresource="Controller.xml"/>
</objects>

8.Main工程里创建LoginControllerLoginService

namespaceMain.Login
{ 
 publicclassLoginController :ApiController
 {
   //依赖注入进来的Service
   publicLoginService Service {get;set; }
   [HttpGet]
   [Route("api/login/index")]
   publicstringIndex()
   {
     returnService.test();
   }
 }
}
namespaceMain.Login.Service
{
 publicclassLoginService
 {
   publicstringtest()
   {
     return"i am LoginService";
   }
 }
}

9.Web工程的根目录下放一个Index.html和一个jquery文件

<!DOCTYPE html>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>
<title></title>
    <metacharset="utf-8" />
<scripttype="text/javascript"src="jquery-1.10.2.min.js"></script>
<scripttype="text/javascript">
 $(function() {
   $.ajax({ url:'api/login/index' })
    .done(function (d, s) { alert(d); })
    .fail(function (xr) { alert(xr.responseText); });
 });
</script>
</head>
<body>
</body>
</html>

10.运行Web工程后得alert显示:i am LoginService


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值