Spirng.NET ASP.NET MVC的依赖注入

Spring.web.mvc 提供对ASP.NET MVC 程序的整合,其中SpringControllerFactory类继承DefaultControllerfacotry


首先需要引用  spring.web.mvc 程序集 ,添加 spring对mvc的依赖注入

在 web.cofnig  configuration节点下配置spring

    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>
    </sectionGroup>

    <spring>
     <context>
      <resource uri="~/Config/Controller.xml"/>
     </context>
    </spring>

Controller xml 文件

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object type="SpringMvcApp.Controllers.HomeController, SpringMvcApp" singleton="false" >
    <property name="Content" value="欢迎使用 ASP.NET MVC 依赖注入" />
  </object>

</objects>

Global 文件重写 SpringMvcApplication 的 RegisterRoutes 方法

  public class MvcApplication : SpringMvcApplication
    {
        protected override void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );

        }
    }
Controller 控制器
 public class HomeController : Controller
    {
        public string Content { get; set; }

        public ActionResult Index()
        {
            ViewData["content"] = this.Content;

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }


   asp.net mvc 页面

 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    主页
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewData["Content"]%></h2>
    <p>
        若要了解有关 ASP.NET MVC 的更多信息,请访问 <a href="http://asp.net/mvc" title="ASP.NET MVC 网站">http://asp.net/mvc</a>。
    </p>
</asp:Content>
posted @ 2011-04-08 14:27  jackyong  阅读(659)  评论(1编辑  收藏  举报