【问题标题】:Parse attributes from MvcHtmlString从 MvcHtmlString 解析属性
【发布时间】:2012-07-13 20:39:06
【问题描述】:

我需要能够解析 MvcHtmlString 中的属性(HtmlHelper 扩展的结果),以便我可以更新和添加它们。

以这个 HTML 元素为例:

<input type="text" id="Name" name="Name" 
 data-val-required="First name is required.|Please provide a first name.">

我需要从data-val-required 中获取值,将其拆分为两个属性,第二个值进入一个新属性:

<input type="text" id="Name" name="Name" 
 data-val-required="First name is required."
 data-val-required-summary="Please provide a first name.">

我正在尝试使用 HtmlHelper 扩展方法来执行此操作,但我不确定解析属性的最佳方法。

【问题讨论】:

  • 哇,让我们从头开始,因为这闻起来/发臭/散发出令人窒息的气味。你到底想达到什么目的?请不要回答:parse attributes from MvcHtmlString。我期待有关您最初目标的答案。因为parse attributes from MvcHtmlString 可能是实现初始目标的一种方式。可能还有其他方法。更好的方法。但是,如果不知道您的初始目标,就很难为您提供那些更好的方法。
  • 好吧,就这个问题而言,我真的只是对如何parsing attributes generated by some other helper 感兴趣。 :) 但是我会开玩笑说,我们的 UX 团队要求为我们的用户提供单独的验证消息——字段级与摘要级。使用内置验证,ModelStateDictionary 仅支持一条消息。因此,在不完全重写内置验证的情况下,这是我能想到的最佳方法。如果您有任何想法,请告诉我,我们可以离线讨论(以免劫持这个话题)。
  • 而且,作为记录,我同意:它确实有味道。很糟糕。
  • 请以其他方式证明我。你会是我最好的朋友。
  • 每个字段需要多个验证消息?我不明白你的要求。告诉我一个用例。

标签: asp.net-mvc asp.net-mvc-3 validation html-helper unobtrusive-validation


【解决方案1】:

实现您想要的一个想法是使用全局操作过滤器。这将获取操作的结果,并允许您在将它们发送回浏览器之前对其进行修改。我使用这种技术将 CSS 类添加到页面的 body 标记中,但我相信它也适用于您的应用程序。

这是我使用的代码(归结为基础):

public class GlobalCssClassFilter : ActionFilterAttribute {
    public override void OnActionExecuting(ActionExecutingContext filterContext) {
        //build the data you need for the filter here and put it into the filterContext
        //ex: filterContext.HttpContext.Items.Add("key", "value");

        //activate the filter, passing the HtppContext we will need in the filter.
        filterContext.HttpContext.Response.Filter = new GlobalCssStream(filterContext.HttpContext);
    }
}

public class GlobalCssStream : MemoryStream {
    //to store the context for retrieving the area, controller, and action
    private readonly HttpContextBase _context;

    //to store the response for the Write override
    private readonly Stream _response;

    public GlobalCssStream(HttpContextBase context) {
        _context = context;
        _response = context.Response.Filter;
    }

    public override void Write(byte[] buffer, int offset, int count) {
        //get the text of the page being output
        var html = Encoding.UTF8.GetString(buffer);

        //get the data from the context
        //ex var area = _context.Items["key"] == null ? "" : _context.Items["key"].ToString();

        //find your tags and add the new ones here
        //modify the 'html' variable to accomplish this

        //put the modified page back to the response.
        buffer = Encoding.UTF8.GetBytes(html);
        _response.Write(buffer, offset, buffer.Length);
    }
}

需要注意的一点是,HTML 被缓冲到 8K 块中,因此如果页面超过该大小,您可能必须确定如何处理。对于我的应用程序,我不必处理这个问题。

此外,由于所有内容都是通过此过滤器发送的,因此您需要确保所做的更改不会影响 JSON 结果等内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 2012-10-14
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多