1、安裝、配置Spring.NET
到官網下載下傳最新版,引入如下兩個dll:Spring.Core.dll、Spring.Web.Mvc4.dll
2、建立MVC項目
先建立控制器:
namespace SpringDemo.Controllers
{
public class UserController : Controller
{
public string Message { get; set; }
public ActionResult Index()
{
ViewBag.Message = Message;
return View();
}
}
}
再建立一個視圖:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<h2>@ViewBag.Message</h2>
</div>
</body>
</html>
3、建立Spring注入配置檔案
在項目根目錄下建立Controller.xml配置檔案,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="SpringDemo.Controllers.UserController,SpringDemo" singleton="false" >
<property name="Message" value="Hello World" />
</object>
</objects>
4、修改Web.config
在configuration節點中增加如下兩個子節點:
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="file://~/Controller.xml"/>
</context>
</spring>
5、修改Global.asax.cs
修改父類為:Spring.Web.Mvc.SpringMvcApplication
6、通路測試
根據項目的實際情況通路Action,會在頁面中顯示:Hello World