我想输出两个不同的视图(一个作为将作为电子邮件发送的字符串),另一个显示给用户的页面.
这是否可以在ASP.NET MVC beta中使用?
我尝试过多个例子:
1. ASP.NET MVC Beta中的RenderPartial到String
如果我使用此示例,则会收到"在HTTP标头发送后无法重定向".
如果我使用它,我似乎无法执行redirectToAction,因为它尝试渲染可能不存在的视图.如果我确实返回了视图,那么它完全搞砸了,看起来根本不正确.
有没有人对我遇到的这些问题有任何想法/解决方案,或者对更好的问题有任何建议?
非常感谢!
以下是一个例子.我要做的是创建GetViewForEmail方法:
public ActionResult OrderResult(string ref)
{
//Get the order
Order order = OrderService.GetOrder(ref);
//The email helper would do the meat and veg by getting the view as a string
//Pass the control name (OrderResultEmail) and the model (order)
string emailView = GetViewForEmail("OrderResultEmail", order);
//Email the order out
EmailHelper(order, emailView);
return View("OrderResult", order);
}
Run Code Online (Sandbox Code Playgroud)
Tim Scott接受的答案(由我改变并格式化):
public virtual string RenderViewToString(
ControllerContext controllerContext,
string viewPath, …Run Code Online (Sandbox Code Playgroud) 我使用RazorEngine解析我的MVC 6项目中的模板,如下所示:
Engine.Razor.RunCompile(File.ReadAllText(fullTemplateFilePath), templateName, null, model);
Run Code Online (Sandbox Code Playgroud)
它适用于测试版6.它在升级到测试版7后出现错误:
MissingMethodException:找不到方法:"Void Microsoft.AspNet.Razor.CodeGenerators.GeneratedClassContext.set_ResolveUrlMethodName(System.String)".在RazorEngine.Compilation.CompilerServiceBase.CreateHost中(类型templateType,类型modelType,String className)
这是global.json:
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-beta7",
"runtime": "clr",
"architecture": "x64"
}
}
Run Code Online (Sandbox Code Playgroud)
这是project.json:
...
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-beta7",
"EntityFramework.Commands": "7.0.0-beta7",
"Microsoft.AspNet.Mvc": "6.0.0-beta7",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta7",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.Facebook": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.Google": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.MicrosoftAccount": "1.0.0-beta7",
"Microsoft.AspNet.Authentication.Twitter": "1.0.0-beta7",
"Microsoft.AspNet.Diagnostics": "1.0.0-beta7",
"Microsoft.AspNet.Diagnostics.Entity": "7.0.0-beta7",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta7",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta7",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta7",
"Microsoft.AspNet.Tooling.Razor": "1.0.0-beta7",
"Microsoft.Framework.Configuration.Abstractions": "1.0.0-beta7",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta7",
"Microsoft.Framework.Configuration.UserSecrets": "1.0.0-beta7",
"Microsoft.Framework.Logging": "1.0.0-beta7",
"Microsoft.Framework.Logging.Console": "1.0.0-beta7",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-beta7",
"RazorEngine": "4.2.2-beta1"
}, …Run Code Online (Sandbox Code Playgroud) 我创建了一个新的MVC6项目并构建了一个新站点.目标是获取视图的渲染结果.我找到了以下代码,但我无法让它工作,因为我找不到ControllerContext和ViewEngines.
这是我想要重写的代码:
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要将Razor页面部分渲染为字符串.
我想创建一个控制器操作,该操作使用包含部分视图和其他可选参数的JSON进行响应.
我熟悉以下将View呈现为字符串的示例:https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs
但是,它与Pages不兼容,因为它只在Views目录中搜索,所以即使我给它一个绝对的路径,它试图找到我的_Layout.cshtml(它甚至不应该这样做!)并失败找到它.
我试图修改它以便渲染页面,但是在尝试渲染时,我最终在部分中获得了ViewData的NullReferenceException.我怀疑它与NullView有关,但我不知道该放在那里(RazorView的构造函数需要许多我不知道如何正确获取的对象).
代码:
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0: https://www.apache.org/licenses/LICENSE-2.0
// Modified by OronDF343: Uses pages instead of views.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
using Microsoft.AspNetCore.Routing;
namespace TestAspNetCore.Services
{
public class RazorPageToStringRenderer
{
private readonly IRazorViewEngine _viewEngine;
private readonly ITempDataProvider _tempDataProvider;
private …Run Code Online (Sandbox Code Playgroud) 我想要的是:
在我的应用程序中,我想为我的电子邮件使用模板。不幸的是,我之前在另一个项目中使用的代码不再起作用。
抛出的错误:
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.'
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题,因为我找不到任何方法来注入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.'
Run Code Online (Sandbox Code Playgroud) 我需要从 HTML Canvas 生成 PDF,但该过程必须在作为 ASP.NET Core Web Api 的服务器端完成。
在标签助手中,我们可以访问标签内部的 HTML。
<!-- Index.cshtml -->
<my-first>
This is the original Inner HTML from the *.cshtml file.
</my-first>
// MyFirstTagHelper.cs > ProcessAsync
var childContent = await output.GetChildContentAsync();
var innerHtml = childContent.GetContent();
Run Code Online (Sandbox Code Playgroud)
如果我们调用视图组件作为标签助手,我们如何访问内部 HTML?
<vc:my-first>
This is the original Inner HTML from the *.cshtml file.
</vc:my-first>
// MyFirstViewComponent.cs > InvokeAsync()
var innerHtml = DoMagic();
Run Code Online (Sandbox Code Playgroud)
一些进一步的想法:
我很欣赏我们可以通过 HTML 属性将任意内容传递给视图组件。但是,在文本量非常大的情况下,这可能变得不切实际,在这种情况下,内部 HTML 会更具可读性并且具有更好的工具支持。
有些人可能还会说,“那么为什么不直接使用标签助手呢?” 好吧,我们想要将视图与标签助手关联起来,而标签助手不支持视图;相反,标签助手要求我们以编程方式构建所有 HTML。
所以我们陷入了困境。一方面,我们想要一个标签助手,但它们不支持视图;另一方面,我们可以使用视图组件作为标签助手,但视图组件可能不允许我们访问内部 HTML。
asp.net-core-mvc tag-helpers asp.net-core asp.net-core-mvc-2.0 asp.net-core-viewcomponent
asp.net-core ×5
c# ×4
asp.net-mvc ×2
razor ×2
asp.net ×1
pdf ×1
razor-pages ×1
rendering ×1
tag-helpers ×1