[英]@Html.Raw equivalent in MVC6
I have several controls that are copy-pasted. 我有几个复制粘贴的控件。 I want to avoid it, so I moved all HTML generating logic in static helper class.
我想避免这种情况,所以我将所有HTML生成逻辑移到了静态帮助器类中。 But when I use it my HTML is escaped in quotes, but I want to display it in raw HTML.
但是,当我使用它时,我的HTML会用引号引起来,但是我想以原始HTML形式显示。 Here is two problems:
这是两个问题:
My current usecase was: 我当前的用例是:
<div class="panel-body panel-body-table">
@(WuiHelper<int>.RenderIntegerInput(Model.Config, x => x.PatchSize, "sourceObserver_patchSize", "Patch Size", 1, 1000000))
</div>
But as I said, it escapes an html in quotes so it renders as plain text (return type of method is string
). 但是正如我所说,它使用引号引起来的html逸出,因此呈现为纯文本(方法的返回类型为
string
)。 It seems that @Html
helper class was removed. 似乎
@Html
帮助器类已被删除。 My current web.config
is: 我当前的
web.config
是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
Here is a screenshot: all namespaces are referenced, but @Html
is missing: 这是一个屏幕截图:引用了所有名称空间,但是缺少
@Html
:
Solved by using IHtmlContent
instead of string
as return value of a method, and wrapping a string in HtmlEncodedString
. 通过使用
IHtmlContent
而不是string
作为方法的返回值并将字符串包装在HtmlEncodedString
。 Entire signature worked: 整个签名有效:
public static IHtmlContent RenderIntegerInput<T>(CustomizableConfig<T> model, Func<T, TParam> func, string id, string name, int minValue, int maxValue)
{
//...
string oldStringResult = ... // generating HTML
return new HtmlEncodedString(oldStringResult);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.