解析来自MvcHtmlString属性 [英] Parse attributes from MvcHtmlString

查看:146
本文介绍了解析来自MvcHtmlString属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够解析从MvcHtmlString属性(一个的HtmlHelper扩展的结果),因此,我可以更新和添加到它们。

I need to be able to parse the attributes from an MvcHtmlString (the results of an HtmlHelper extension), so that I can update and add to them.

例如,以此HTML元素:

For example, take this HTML element:

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

我需要采取从值数据-VAL-需要与第二值进入一个新的属性将它分成两个属性:

I need to take the value from data-val-required, split it into two attributes with the second value going into a new attribute:

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

我想用的HtmlHelper扩展方法来做到这一点,但我不知道解析属性的最佳方式。

I'm trying to do this with an HtmlHelper extension method, but I'm not sure the best way to parse the attributes.

推荐答案

一个想法来完成你想要的是使用全球行动过滤器。这将采取行动的结果,并允许你修改他们,他们被送回浏览器之前。我用这个技术,CSS类添加到页面的body标签,但我相信它会为你的应用程序工作了。

One idea to accomplish what you want is to use a global action filter. This will take the results of the action and allow you to modify them before they are sent back to the browser. I used this technique to add CSS classes to the body tag of the page, but I believe it will work for your application too.

下面是code(归结为基础),我用:

Here is the code (boiled down to the basics) that I used:

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的块,所以你可能不得不决定如何处理,如果你有一个尺寸过大的页面。对于我的申请,我没得面对这一切。

One thing to watch out for is that the HTML is buffered into, I believe, 8K chunks, so you might have to determine how to deal with that if you have pages over that size. For my application, I didn't have to deal with that.

此外,因为一切都是通过这个过滤器发送的,你需要确保你进行更改不会影响对事物之类的东西JSON结果。

Also, since everything is sent through this filter, you need to be sure that the changes you are making won't effect things on things like JSON results.

这篇关于解析来自MvcHtmlString属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆