首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。