3

In my MVC3 application I want to remove all HTML5 tags for the output when the user is using IE < 9 to avoid using a frontend workaround.

I've looked in to using a HttpModule, ActionFilter, the OnResultExecuted method on a controller and inside Application_Start.

I've figured so far that I need to get the output as a string from HttpApplication.Context.Response.OutputStream using something like:

HttpApplication application = (HttpApplication)source;
HttpResponse response = application.Context.Response;
StreamReader sr = new StreamReader(stream);
string content = sr.ReadToEnd();

But all I get is the same error Stream was not readable. I can write to the response with context.Response.Write.

From reading around on SO and google, MVC doesn't seem to have the same "page life cycle" has webforms(where I'm just overring Render and it works fine) which makes sens.

So my question is how do I get the HTML as a String in MVC? Have anyone tried to manipulate the html output?

4 Answers 4

7

I think you should be able to use an ActionFilter to do this. I've seen an example of someone modifying the output stream in this blog post. or in this post.

Sign up to request clarification or add additional context in comments.

1 Comment

The last link is everything everyone should know about changing HTML output! Thanks!
3

HTML5 is backwards compatible so feel more than free to use this default output with any browser. Obsolete browsers such as IE6 would still render correctly and even unobtrusive validation is going to work. So my advice is to leave the output as is.

5 Comments

Unfortunately no, tags unrecognized by IE will be closed and ignored. With javascript you can create the tags so they may still be used as container elements. I don't want to wrap all html5 tags to keep the layout.
@fredrik, of course that if you use HTML5 specific tags such as <canvas> they will be ignored by IE6, but in ASP.NET MVC 3 all that's HTML5 specific are the data-* attributes which are applied to standard tags such as <input> which IE6 is perfectly comfortable with.
well yes. They are ignored. If you have <article><p>Lorem</p></article> it will be rendered as <article /><p>Lorem </p> in IE. Ending up messing with the layout
@fredrik, ASP.NET MVC 3 never uses tags like this. That's markup which you have explicitly placed somewhere.
Yes. I use HTML5 also for semantic markup in my views. But found the solution.
0

Why dont you use javascript? Just determine what is user browser and remove html you want. JQuery has nice methods to do that.

1 Comment

You absolutely cloud, but unfortunately this would require javascript and application I'm building should work without it.
0

To follow the concept of the MVC pattern, I'd look to use a different View implementation depending on whether the browser supports HTML 5 or not rather than trying to bodge the output. An alternative would be to use controls through a third party library that can decompose gracefully when the browser doesn't support all the latest features (or the library can implement the controls independently). JQuery, as supplied with ASP.NET MVC is an ideal with many, many controls available that cover most HTML5 tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.