码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net MVC--Controller激活2

时间:2016-12-29 10:38:21      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:not   request   factory   var   one   log   www   html   stc   

一、激活实例代码1

这是在Global文件中使用

var routeData = 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 = 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

标签:not   request   factory   var   one   log   www   html   stc   

原文地址:http://www.cnblogs.com/tianma3798/p/6231907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!