C# 在core 3.0中将视图呈现为字符串:找不到与ActionContext关联的IRouter
我想要什么:C# 在core 3.0中将视图呈现为字符串:找不到与ActionContext关联的IRouter,c#,asp.net-core,asp.net-core-3.0,C#,Asp.net Core,Asp.net Core 3.0,我想要什么: Could not find an IRouter associated with the ActionContext. If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.
Could not find an IRouter associated with the ActionContext.
If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
在我的应用程序中,我想为我的电子邮件使用一个模板。不幸的是,我以前在另一个项目中使用的代码不再有效
引发的错误:
Could not find an IRouter associated with the ActionContext.
If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
我不知道如何解决这个问题,因为我找不到任何方法来注入IUrlHelper
。我不确定为什么需要这样做,因为它无论如何都不在视图中
字符串呈现方法:
Could not find an IRouter associated with the ActionContext.
If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
公共异步任务RenderToStringAsync(字符串视图名称,对象模型)
{
var httpContext=newdefaulthttpcontext{RequestServices=\u serviceProvider};
var actionContext=new actionContext(httpContext,new RouteData(),new ActionDescriptor());
使用(var sw=new StringWriter())
{
var viewResult=FindView(actionContext,viewName);
如果(viewResult==null)
{
抛出新ArgumentNullException($“{viewName}与任何可用视图都不匹配”);
}
var viewDictionary=new ViewDataDictionary(new EmptyModelMetadataProvider(),new ModelStateDictionary())
{
模型
};
var viewContext=新的viewContext(
行动背景,
查看结果,
查看字典,
新的TempDataDictionary(actionContext.HttpContext,_tempDataProvider),
西南,
新的HtmlHelpOptions());
等待viewResult.RenderAsync(viewContext);//抛出错误找到解决方案: 我认为,由于创建ActionContext并为其提供一个空的RoutedData对象会给我一个IRouter异常,因此下一个最佳解决方案是看看是否可以从实际请求中使用HttpContext 因此,通过依赖项注入,我添加了httpContextAccessor并使用了可用的HttpContext对象 为完整起见,我将分享最终实现: 将视图呈现为HTML:
Could not find an IRouter associated with the ActionContext.
If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
RenderToString(字符串,对象)
var htmlBody=await Renderer.renderString($“/Views/Shared/confirm email.cshtml”,model);
服务:
Could not find an IRouter associated with the ActionContext.
If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection
and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.'
公共类ViewRenderService:IViewRenderService
{
私有只读IRazorViewEngine(U razorViewEngine);
私有只读ITempDataProvider _tempDataProvider;
专用只读IHttpContextAccessor\u contextAccessor;
公共视图渲染服务(IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IHttpContextAccessor(上下文访问器)
{
_razorViewEngine=razorViewEngine;
_tempDataProvider=tempDataProvider;
_contextAccessor=contextAccessor;
}
公共异步任务RenderToString(字符串视图名称,对象模型)
{
var actionContext=new actionContext(_contextAccessor.HttpContext,_contextAccessor.HttpContext.GetRouteData(),new ActionDescriptor());
使用var sw=new StringWriter()等待;
var viewResult=FindView(actionContext,viewName);
如果(viewResult==null)
{
抛出新ArgumentNullException($“{viewName}与任何可用视图都不匹配”);
}
var viewDictionary=new ViewDataDictionary(new EmptyModelMetadataProvider(),new ModelStateDictionary())
{
模型
};
var viewContext=新的viewContext(
行动背景,
查看结果,
查看字典,
新的TempDataDictionary(actionContext.HttpContext,_tempDataProvider),
西南,
新的HtmlHelpOptions()
);
等待viewResult.RenderAsync(viewContext);
返回sw.ToString();
}
私有IView FindView(ActionContext ActionContext,字符串viewName)
{
var getViewResult=_razorViewEngine.GetView(executingFilePath:null,viewPath:viewName,isMainPage:true);
if(getViewResult.Success)
{
返回getViewResult.View;
}
var findViewResult=_razorViewEngine.FindView(actionContext,viewName,isMainPage:true);
如果(findViewResult.Success)
{
返回findViewResult.View;
}
var searchedLocations=getViewResult.searchedLocations.Concat(findViewResult.searchedLocations);
var errorMessage=string.Join(
环境,新线,
new[]{$”找不到视图“{viewName}”。搜索了以下位置:“}.Concat(searchedLocations));
抛出新的InvalidOperationException(errorMessage);
}
}
现在它工作得很好。你能分享你的
FindView
方法吗?你是如何定义\u serviceProvider
和\u tempDataProvider
?这是什么=>IViewRenderService?!@MahmodAbuJehad那只是一个接口。