1、新增加一个类库,命名为CMS.Common
引用:
Spring.Core
System.Web.MVC
System.Web.Abstractions
System.Web.Routing;
在里面创建一个类:
SpringControllerFactory:
Code
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Core.IO;
using Spring.Objects.Factory;
using Spring.Objects.Factory.Xml;
using System.Web.Mvc;
using System.Web.Routing;
using System.IO;
using Spring.Context;
using Spring.Context.Support;
namespace CMS.Common
{
/// <summary>
/// 控制器工厂
/// </summary>
public class SpringControllerFactory : IControllerFactory
{
//private static log4net.ILog log = log4net.LogManager.GetLogger(typeof(SpringControllerFactory));
/// <summary>
/// Default ControllerFactory
/// </summary>
private static DefaultControllerFactory defalutf = null;
public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
{
//get spring context
IApplicationContext ctx = ContextRegistry.GetContext();
if (ctx.ContainsObject(controllerName))
{
object controllerf = ctx.GetObject(controllerName);
return (IController)controllerf;
}
else
{
if (defalutf == null)
{
defalutf = new DefaultControllerFactory();
}
return defalutf.CreateController(requestContext, controllerName);
}
}
public void ReleaseController(IController controller)
{
//get spring context
IApplicationContext ctx = ContextRegistry.GetContext();
if (!ctx.ContainsObject(controller.GetType().Name.Replace("Controller", "")))
{
if (defalutf == null)
{
defalutf = new DefaultControllerFactory();
}
defalutf.ReleaseController(controller);
}
}
}
}
1、新增加一个类库,命名为CMS.Controllers 用来存放MVC的控制器
引用:
System.Web.MVC
System.Web.Abstractions
新增加一个控制器
HomeController:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace CMS.Controllers
{
public class HomeController : System.Web.Mvc.Controller
{
public ActionResult Index()
{
ViewData["Title"] = "用户中心 -- Haphere.com";
return View();
}
}
}
在CMS.MVCWeb网站中加入对CMS.Common和CMS.Controllers的引用
配置Global.asax
原本代码:
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
新增代码:
Code
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(
typeof(CMS.Common.SpringControllerFactory));
RegisterRoutes(RouteTable.Routes);
}
删除CMS.MvcWeb网站下的Controllers文件夹。
配置完成!
源码下载