spring.net开发webMVC项目(入门)
新建.net WebMVC项目
初次使用.net开发,记录一下。
使用VS新建一个ASP.NET MVC项目。
写一些Controller随便测试一下。
项目结构都是默认的。
配置方法也是网上查的,写的太不清晰了,重新理一遍。
引入spring.net
在VS中右击项目选择“管理nuget包”,搜索“spring”,选择spring core安装。
配置spring
在Global.asax中修改一处:继承改为SpringMvcApplication,当然为了使用这个,需要using using Spring.Web.Mvc;
修改Web.config文件
紧邻着<configSections>下面添加如下
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
然后在 configuration中加入配置
<!--2、配置spring-->
<spring>
<context>
<resource uri="config://spring/objects" />
<!--spring.xml是新建的文件,与Web.config同级注意路径-->
<resource uri="file://~/spring.xml" />
</context>
<objects xmlns="http://www.springframework.net">
</objects>
</spring>
使用spring
一般在controller中需要注入service层用于操作数据,所以,先编写一个提供服务的类UserOperater。路径是WebApplication1.SqlSugarTest.factory.UserOperater。
然后写一个Controller并有一个属性
public UserOprater userOprater { get; set; }
并没有初始化
使用“没有初始化的”userOperater:
注册到容器中
<object name="userOperater"
type="WebApplication1.SqlSugarTest.factory.UserOperater">
</object>
<object type="WebApplication1.MySqlData.MySqlTestController">
<property name="userOprater" ref="userOperater"/>
<!--Controller引用-->
</object>
F5启动项目测试。
$.get(
"/MySqlTest/GetUserById",
{"id":"1"},
function(data){
console.log(data);
}
);
可以正常得到数据
附上项目目录