Spring.Web.Mvc 注入(控制器属性注入)

1.web.config配置

<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>
</sectionGroup>
</configSections>

<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>

<spring>
<context>
<resource uri="~/Config/Controllers.xml"/>
</context>
</spring>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
</compilers>
</system.codedom>
</configuration>

2.根目录添加lib文件夹(spring.core spring.web.mvc Common.Logging.dll Common.Logging.Log4Net.dll)

程序集引用添加这4个东西

3.mvcapplication 注册路由重写 继承SpringMvcApplication

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Spring.Web.Mvc;

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

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
//protected void Application_Start()
//{
// AreaRegistration.RegisterAllAreas();
// RouteConfig.RegisterRoutes(RouteTable.Routes);
//}
}
}

3.根目录下新建Config文件夹 里面添加Controllers.xml(右键属性 始终复制 我的迷信)

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

<object type="WebApplication12.Controllers.HomeController, WebApplication12" singleton="false" >
<property name="Message1" value="海贼王的乱入" />
</object>
  <!--集合注入-->

<object type="WebApplication12.Controllers.HomeController, WebApplication12" singleton="false" >
<property name="Message" >
<list element-type="System.String">
<value>B0 Stage</value>
<value>B1 Stage</value>
<value>B2 Stage</value>
<value>B3 Stage</value>
<value>B4 Stage</value>
<value>B5 Stage</value>
<value>B6 Stage</value>
</list>
</property>
</object>

</objects>

4.控制器添加Message属性之后用于注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication12.Controllers
{
public class HomeController : Controller
{
public string Message { get; set; }
// GET: Home
public ActionResult Index()
{
ViewData["Message"] = Message;
return View();
}
}
}

4.视图调用


@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<h1>@ViewData["Message"]</h1>
</div>
</body>
</html>

 

posted on 2015-09-20 12:54  听哥哥的话  阅读(668)  评论(0编辑  收藏  举报

导航