配置spring.net+mvc花了将近一天的时间,现在把之前遇到的问题列下来。
首先安装在程序包控制器安装:
install-package spring.core
install-package spring.web
install-package spring.web.mvc5
install-package spring.web.extensions
添加配置文件:controllers.xml和services.xml
Global.asax里修改继承类:Spring.Web.Mvc.SpringMvcApplication
web.config配置:
<!--spring.net+mvc配置-->
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<!--EF配置-->
<connectionStrings>
<add name="DataModelContainer" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=aotoBox; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="aotoBoxEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=" MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<!--spring.net的容器的配置节点-->
<spring>
<context>
<resource uri="file://~/Config/services.xml" />
<!--有顺序的-->
<resource uri="file://~/Config/controllers.xml" />
</context>
</spring>
controller里添加:
public UserInfoService userInfoService { get; set; }
到这里一般就完成了,但一个个坑出现了
其中两个:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
未能加载文件或程序集“System.Web.Http.WebHost, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
解决方法:引用System.Web.Http,System.Web.Http.WebHost两个DLL即可,没有的 话
Install-Package Microsoft.aspNet.WebApi这样也可以得到
2.Spring.Core.InvalidPropertyException: 'UserInfoService' node cannot be resolved for the specified context ...
这个搞了一上午,最终。。。
<object name="controllers" type="Solution.OA.UI.Portal2.Controllers.UserInfoController, Solution.OA.UI.Portal2" singleton="false" ><!--singleton代表是否单例模式,默认是TRUE-->
<!--<property name="Message" value="Welcome to ASP.NET MVC3 powered by Spring.NET!" />-->
<property name="userInfoService" ref="userInfoService1" />
</object>
controllers配置文件里的name=userInfoService需与controller的属性一致