简体   繁体   English

MVC6中的@ Html.Raw等效项

[英]@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: 这是两个问题:

  1. I cannot extend controller itself, I should do it from a view only. 我不能扩展控制器本身,我应该仅从一个角度来看。
  2. I cannot use tag helpers, becuase one of parameters is of delegate type, so it cannot be passed to tag helper class (as far as I know). 我不能使用标签助手,因为其中一个参数是委托类型,所以它不能传递给标签助手类(据我所知)。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM