Parse attributes from MvcHtmlString

China☆狼群 提交于 2019-12-12 07:03:20

问题


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.

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.">

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.">

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


回答1:


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.

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);
    }
}

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.

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.



来源:https://stackoverflow.com/questions/11478035/parse-attributes-from-mvchtmlstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!