MVC5应用中必要的dll如下:
Common.Logging.dll
Spring.Core.dll
Spring.Web.dll
Spring.Web.Mvc5.dll
Spring.Web.Extensions.dll
System.Web.Http.dll//没有这个会报后面的错误
通过Nuget下载 spring.web.mvc5,Spring.web.extensions,microsoft.aspnet.webapi
配置spring.net
在MVC项目下添加Config文件夹,新增俩个xml:controllers.xml,services.xml
controllers.xml:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="DXM.OA.WebApp.Controllers.UserInfoController, DXM.OA.WebApp" singleton="false" >
<property name="UserInfoService" ref="userInfoService" />
</object>
<!--抽取到services.xml-->
<!--<object type="DXM.OA.BLL.UserInfoService, DXM.OA.BLL" singleton="false" name="userInfoService" >
</object>-->
</objects>
services.xml:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="DXM.OA.BLL.UserInfoService, DXM.OA.BLL" singleton="false" name="userInfoService" >
</object>
</objects>
右键属性,将这俩个xml的输出目录改为总是输出,如下:
修改web.config
在configuration下的第一个节点添加:
<configuration>
..............
<!--spring.net 配置开始-->
<configSections> //该节点必须是第一个节点
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="file://~/Config/controllers.xml" />
<resource uri="file://~/Config/services.xml" />
</context>
</spring>
<!--spring.net 配置结束-->
</configuration>
修改Global.asax.cs的父类,添加测试代码
将System.Web.HttpApplication改为:Spring.Web.Mvc.SpringMvcApplication
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace DXM.OA.WebApp
{
public class MvcApplication : SpringMvcApplication//System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
运行程序,发现一个错误:
原因为未引用System.Web.Http,可添加dll引用,也可以再nuget包中添加microsoft.aspnet.webapi:
运行程序,成功: