ASP.NET MVC4 中整合 Spring.NET 1.2.10

25 篇文章 0 订阅
22 篇文章 0 订阅

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






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值