ASP.NET Core调用另一个控制器并生成返回视图的html


C# 全选
var articleController = this.HttpContext.RequestServices.GetService(typeof(YESCMS.Controllers.ArticleController)) as YESCMS.Controllers.ArticleController;
//获取结果
var actionResult = articleController.DocList(v.Category);

//判断结果是否是一个ViewResult
if (actionResult is ViewResult)
{
	ViewResult viewResult = actionResult as ViewResult;


	var routeData = new RouteData();
	routeData.Values.Add("controller", "Article");
	routeData.Values.Add("action", "DocList");
	var serviceProvider = this.HttpContext.RequestServices.GetService(typeof(IServiceProvider)) as IServiceProvider;


	var httpContext = new DefaultHttpContext { RequestServices = serviceProvider };
	//var actionContext = new ActionContext(httpContext, routeData, new ActionDescriptor());
	var actionContext = new ActionContext(httpContext, routeData, articleListAction);

	//actionContext.ActionDescriptor = articleListAction;
	//下面的代码就是执行这个ViewResult,并把结果的html内容放到一个StringBuiler对象中
	var services = this.HttpContext.RequestServices;
	var viewEngine = services.GetService(typeof(Microsoft.AspNetCore.Mvc.ViewEngines.ICompositeViewEngine)) as Microsoft.AspNetCore.Mvc.ViewEngines.ICompositeViewEngine;
	Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult result = viewEngine.FindView(actionContext, viewResult.ViewName, true);

	//var executor = services.GetRequiredService<ViewResultExecutor>();
	var option = services.GetRequiredService<IOptions<MvcViewOptions>>();
	//var result = executor.FindView(context, viewResult);
	result.EnsureSuccessful(originalLocations: null);
	var view = result.View;
	StringBuilder builder = new StringBuilder();

	var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
	viewDictionary.Model = viewResult.Model;

	var tempDataProvider = services.GetService<ITempDataProvider>();
	var tempData = new TempDataDictionary(actionContext.HttpContext, tempDataProvider);

	using (var writer = new StringWriter(builder))
	{
		var viewContext = new ViewContext(
			actionContext,
			view,
			viewDictionary,
			tempData,
			writer,
			option.Value.HtmlHelperOptions);

		view.RenderAsync(viewContext).GetAwaiter().GetResult();
		//这句一定要调用,否则内容就会是空的
		writer.Flush();
	}
	string html = builder.ToString();
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
管理员
上一篇:SQLite数据库删除数据后数据库文件大小不变
下一篇:monaco-editor使用指南
评论列表

发表评论

评论内容
昵称:
关联文章

ASP.NET Core调用一个控制器生成返回视图html
ASP.NET Core MVC 在过滤器ActionFilter中保存页面生成html静态页面文件
从数据库或者其他位置加载ASP.NET MVC Views 视图 数据库中加载 cshtml
asp.net - 在 ASP.NET Core MVC 中嵌套 TagHelper
【推荐】Razor文件编译 ASP.NET Core
ASP.NET Core官网教程,资料查找
ASP.NET Core统一接口返回模型
ASP.NET MVC 后台控制器向View前台传递数据几种方式
C# ASP.NET Core开发学生信息管理系统(一)
asp.net core MVC路由添加.html伪静态url时报错
ASP.NET Core MVC中路由约束
ASP.NET MVC快速入门(一)
ASP.NET+MVC入门踩坑笔记 (一) 创建项目 项目配置运行 以及简单Api搭建
C# ASP.NET Core开发学生信息管理系统(三)
C# ASP.NET Core开发学生信息管理系统(二)
ASP.NET Core web API中使用Swagger/OpenAPI(Swashbuckle)
.NET5 ASP.NET Core 添加API限流
ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action
ASP.NET Core 中读取Post Request.Body 正确姿势
.NET Core生成后没有Nugetdll文件

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码