首页 > 代码库 > Asp.Net MVC--Controller激活2

Asp.Net MVC--Controller激活2

一、激活实例代码1

这是在Global文件中使用

var routeData = http://www.mamicode.com/new RouteData();
routeData.Values["controller"] = "Sys";
routeData.Values["action"] = "NotFound";
var requestContext = new RequestContext(new HttpContextWrapper(httpContext), routeData);
var controller = ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, "Sys") as SysController;
//controller.ViewData.Model=model;
(controller as IController).Execute(requestContext);
ControllerBuilder.Current.GetControllerFactory().ReleaseController(controller);

激活实例代码2

这是在controller中的情况下

string controllerName = this.RequestContext.RouteData.Controller;  
IControllerFactory controllerFactory = ControllerBuilder.Current.GetControllerFactory();  
IController controller = controllerFactory.CreateController(this.RequestContext, controllerName);  
  
controller.Execute(this.RequestContext); 

实例代码3,激活指定controller中的action并返回视图到当前上下文:

        //输出指定controller下的内容
        RouteData routeData = http://www.mamicode.com/new RouteData();
        routeData.Values.Add("controller", "ViewOne");
        routeData.Values.Add("action", "TempTwo");
        routeData.Values.Add("name", "zhangsan");
        IController one = new ViewOneController();
        one.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));

 

更多:

http://www.cnblogs.com/tianma3798/p/3958575.html

关于上下文

Asp.Net MVC--Controller激活2