ASP.NET_MVC使用Spring.Net.MVC依赖注入学习笔记
2015-08-11 20:12
1096 查看
Spring.NET 1.3.1的程序集Spring.Web.Mvc提供对ASP.NET MVC程序的整合。其中SpringControllerFactory类继承自DefaultControllerFactory,以便于实现Spring.NET对Controller的管理
先到 http://www.springframework.net/ 官方网站下载 源码
可以看到 源码dll目录下有
Spring.Web.Mvc.dll就是我们需要的 dll类库
1,新建一个ASP.NET MVC4项目,.NET4.0版本,项目名称为 Spring_Net依赖注入MvcDemo
模板选择【基本】,点击确定
添加 Spring.Web.Mvc.dll
Common.Logging.dll
Spring.Core.dll的引用
2,在App_Start文件夹上右键新建一个类,命名为 SpringMvcApplication.cs
分别添加以下命名空间
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
SpringMvcApplication.cs的完整代码如下
3,打开Global.asax,修改 MvcApplication 让它继承自 SpringMvcApplication
并添加以下引用
using Spring_Net依赖注入MvcDemo.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
Global.asax修改后 完整代码如下
4,新建一个空控制器,HomeController.cs
添加以下代码
并添加对应的试图,写入 ViewData["Message"]
Index.cshtml代码如下
5,增加Controller配置Controllers.xml
在主目录新建一个文件夹,命名为Config
新建一个 xml文件,命名为 Controllers.xml
添加以下xml文档
<object type="Spring_Net依赖注入MvcDemo.Controllers.HomeController, Spring_Net依赖注入MvcDemo" singleton="false" >
<property name="Message" value="Spring.NET 1.3.1 的 ASP.NET MVC 依赖注入" />
</object>
6,配置Web.config
修改 Global.asax 所在主目录下的 Web.config【注意,非View下的】
在第一个 configSections正下面 加入
在第一个 </configSections> 结束标签正后面 加入
7,运行调试
可以看到
则配置 Spring.Net ASP.NET MVC成功!
先到 http://www.springframework.net/ 官方网站下载 源码
可以看到 源码dll目录下有
Spring.Web.Mvc.dll就是我们需要的 dll类库
1,新建一个ASP.NET MVC4项目,.NET4.0版本,项目名称为 Spring_Net依赖注入MvcDemo
模板选择【基本】,点击确定
添加 Spring.Web.Mvc.dll
Common.Logging.dll
Spring.Core.dll的引用
2,在App_Start文件夹上右键新建一个类,命名为 SpringMvcApplication.cs
分别添加以下命名空间
using Spring.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
SpringMvcApplication.cs的完整代码如下
using Spring.Web.Mvc; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace Spring_Net依赖注入MvcDemo.App_Start { public abstract class SpringMvcApplication : HttpApplication { /// <summary> /// Handles the Start event of the Application control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected virtual void Application_Start(object sender, EventArgs e) { RegisterAreas(); RegisterRoutes(RouteTable.Routes); } /// <summary> /// Configures the <see cref="Spring.Context.IApplicationContext"/> instance. /// </summary> /// <remarks> /// You must override this method in a derived class to control the manner in which the /// <see cref="Spring.Context.IApplicationContext"/> is configured. /// </remarks> protected virtual void ConfigureApplicationContext() { } /// <summary> /// Executes custom initialization code after all event handler modules have been added. /// </summary> public override void Init() { base.Init(); //the Spring HTTP Module won't have built the context for us until now so we have to delay until the init ConfigureApplicationContext(); RegisterSpringControllerFactory(); } /// <summary> /// Registers the areas. /// </summary> /// <remarks> /// Override this method in a derived class to modify the registered areas as neeeded. /// </remarks> protected virtual void RegisterAreas() { AreaRegistration.RegisterAllAreas(); } /// <summary> /// Registers the routes. /// </summary> /// <remarks> /// Override this method in a derived class to modify the registered routes as neeeded. /// </remarks> protected virtual void RegisterRoutes(RouteCollection routes) { // This IgnoreRoute call is provided to avoid the trouble of CASSINI passing all req's thru // ASP.NET (and thus the controller pipeline) during debugging // see http://stackoverflow.com/questions/487230/serving-favicon-ico-in-asp-net-mvc and elsewhere for more info routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } /// <summary> /// Registers the controller factory with the Mvc Framework. /// </summary> protected virtual void RegisterSpringControllerFactory() { ControllerBuilder.Current.SetControllerFactory(typeof(SpringControllerFactory)); } } }
3,打开Global.asax,修改 MvcApplication 让它继承自 SpringMvcApplication
并添加以下引用
using Spring_Net依赖注入MvcDemo.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
Global.asax修改后 完整代码如下
using Spring_Net依赖注入MvcDemo.App_Start; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace Spring_Net依赖注入MvcDemo { // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明, // 请访问 http://go.microsoft.com/?LinkId=9394801 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 } // 参数默认值 ); } /* protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } */ } }
4,新建一个空控制器,HomeController.cs
添加以下代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Spring_Net依赖注入MvcDemo.Controllers { public class HomeController : Controller { // // GET: /Home/ public string Message { get; set; } public ActionResult Index() { ViewData["Message"] = this.Message; return View(); } } }
并添加对应的试图,写入 ViewData["Message"]
Index.cshtml代码如下
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> ViewData["Message"] </div> </body> </html>
5,增加Controller配置Controllers.xml
在主目录新建一个文件夹,命名为Config
新建一个 xml文件,命名为 Controllers.xml
添加以下xml文档
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net">
<object type="Spring_Net依赖注入MvcDemo.Controllers.HomeController, Spring_Net依赖注入MvcDemo" singleton="false" >
<property name="Message" value="Spring.NET 1.3.1 的 ASP.NET MVC 依赖注入" />
</object>
</objects>
6,配置Web.config
修改 Global.asax 所在主目录下的 Web.config【注意,非View下的】
在第一个 configSections正下面 加入
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=152368 --> <configuration> <configSections> <!--依赖注入开始--> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/> </sectionGroup> <!--依赖注入End--> 。。。。 。。。。 。。。。
在第一个 </configSections> 结束标签正后面 加入
。。。。 。。。。 </configSections> <!--依赖注入开始--> <spring> <context> <resource uri="~/Config/Controllers.xml"/> </context> </spring> <!--依赖注入End--> <connectionStrings> 。。。。 。。。。
7,运行调试
可以看到
则配置 Spring.Net ASP.NET MVC成功!
相关文章推荐
- ASP.NETMVCView页面内判断是否登录
- ASP.NET_MVC4_使用ViewModel给前台传递多个model
- 如何制作一个ASP.NETMVC4信息发布网站
- ASP.NET 页面禁止被 iframe 框架引用
- VS EF Error: Configuration Error extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"
- android--asp.net webservice 返回json
- ASP如何计算中英文混合字符串长度和截取字符串 .
- 用Metasploit破解Mysql用户名和密码
- asp.net 父窗体获取子窗体的返回值,可用来对父窗体局部更新
- ASP.NET验证码(3种)
- ASP.NET WebForm
- Asp.Mvc 2.0实现用户注册实例讲解(1)
- ASP.NET MVC4之js css文件合并功能(3)
- ASP.NET MVC3模板页的使用(2)
- 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据
- 简析ASP.NET网站的创建与发布过程
- 七天学会ASP.NET MVC(七)――创建单页应用
- 七天学会ASP.NET MVC(七)——创建单页应用
- 七天学会ASP.NET MVC(七)——创建单页应用
- ASP.NET Web Api 实践系列(二)Get/Post方式调用Web Api