Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

How can I stop ASP.NET rewriting/breaking my XHTML code?

Hiya,

I'm currently trying to port some legacy VBScript applications to C#, and I've discovered that ASP.NET rewrites my XHTML code so that is it displays correctly in various browsers.  Now, I find this slightly patronising, as my code already works correctly in everything from IE5 to Safari 3, and .NET is, in fact, breaking the output.

Is there any way to stop it doing this?  I'm using Visual Studio 2005, which I gather may be doing some things sneakily when I compile the code, though I've not managed to find any project preferences which allow me to disable this.

I understand why Microsoft have set  ASP.NET up to do this, but I would rather have control over the output of my own code, as I'm not particularly au fait with the idea that the interpreter is outputting code over which I have no control.  I've already added the <xhtmlConformance mode="Strict" /> to the web.config file, but this doesn't do much for correcting the output in browsers such as IE5.5 and 6, and Firefox occasionally gets a few odd things as well, breaking the XML parser, since XHTML is output as just that to the browsers which can accept it.

Thanks in advance,

Ben.
0
Illarane
Asked:
Illarane
  • 5
  • 4
  • 4
1 Solution
 
McExpCommented:
Please explain which XHTML it is modifiying, I've not seen any examples of this sort of rewriting in my own projects.
0
 
steveberzinsCommented:
see this:

http://msdn2.microsoft.com/en-us/library/exc57y7e.aspx

lots of info on xhtml and asp.net.

recommends adding the DOCTYPE declaration, and the ContentType on the @Page, have you tried those things?

this article makes it sound like a browser may not behave as expected without doing this, and it may not be asp.net itself.
0
 
IllaraneAuthor Commented:
McExp: When you view code in a browser like Firefox or Lynx, the .NET parser rewrites your HTML code so that it will work in legacy browsers.  The resulting output is barely HTML4, let alone XHTML, and usually breaks layouts, etc.  Div tags get converted to tables in some cases, and it insists on inserting name tags into form elements.  Adding the <xhtmlConformance mode="Strict" /> tag to the web.config file seems to clean it up somewhat, but it is still rewriting my code when no rewriting is actually required.

steveberzins: My code has an XHTML 1.1 DOCTYPE on the first line, unless the browser supports application/xhtml+xml, in which case it outputs <?xml version="1.0" enctype="utf-8" ?> before it, along with an application/xhtml+xml Content-Type header.  This seems to have little to no impact on the output.  I gather that the ContentType is not rewritten, and past experiences of trying to feed IE6 application/xhtml+xml has always resulted in much pain.

The issue here is not the browser displaying my code incorrectly; that I can deal with.  My gripe is ASP.NET rewriting my code and sending broken HTML to a browser, breaking a layout which has already been tailored to work in it.  It's this I want to disable.  When I navigate to a page, I want to see my code in the source, not a marred version produced by a parser which thinks it knows better than me.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
McExpCommented:
Ok, I was told (and in my experiences) the only controls that render differently on different browsers are the asp custom controls. From what I've seen If you type code directly into the page (XHTML HTML or utter giberish) nothing happens to it!
0
 
IllaraneAuthor Commented:
No, it definitely rewrites things.  It's also inserting hidden inputs into my forms for which I didn't ask. :)
0
 
steveberzinsCommented:
have you tried using a tool like fiddler, or something like it, to see what is actually sent to the browser, to make sure it is the server messing it up, could be IE, seems unlikely, but worth a sanity check, in case no matter what you do on the server, IE, (or any browser) won't listen right. :)
0
 
McExpCommented:
have you checked to see what these hidden fields are added to support, I'm not willing to belive they are added for no good reason. One of the big selling points of the ASP.NET development model is that you layout the pages with the required server controls and don't get bogged down in standards compliance issues.
0
 
steveberzinsCommented:
they are probably the fields added for viewstate, and postback support.

and adding them, is probably not the problem, I'm guessing that if you've got the correct content type, and DOCTYPE, I would hope that asp.net would render them xhtml compliant.

what browsers are you having problems with?
have you checked your browser capabilities file?

and, found this while digging around...

Overriding Browser Type Detection

If you want to explicitly control how the page is rendered instead of relying on automatic browser detection, you can set the page's ClientTarget property. You can set the property declaratively as an attribute of the @ Page directive for that page, or you can set it programmatically.

The value of the ClientTarget property is an alias for the type of browser you want to render the page for. For example, to force the page to render for Microsoft Internet Explorer 6.0, you can use the alias uplevel. The alias must be defined in the clientTarget Element (ASP.NET Settings Schema) section of the configuration file. The predefined defaults are ie5, ie4, uplevel, and downlevel.

By specifying the alias downlevel, you can force the page to render HTML 3.2-compatible elements no matter what browser has requested the page. Similarly, by specifying uplevel, you can force the page to render CSS style attributes even for older browser versions.

You can create additional aliases by defining them in the Machine.config or Web.config file, which enables you to create custom browser definitions. For more information, see ASP.NET Configuration Settings.


0
 
steveberzinsCommented:
oh yea, probably good give you where this (above) info came from

http://msdn2.microsoft.com/en-us/library/x3k2ssx2.aspx
0
 
IllaraneAuthor Commented:
steveberzins: I've got a network monitor, which is showing the code that's being sent.  It's definitely being rewritten by ASP.NET, as the original VBScript code outputs exactly as I want it to.

McExp: The hidden fields are session and event data associated with stuff I'm never going to use.  As I said before, I understand why Microsoft have done things this way, I'd just rather they didn't.  Or at least gave me the option to switch it off.  I'm pretty sure I know better than a piece of software how to display my website to different clients in order for it to appear the same. ;)
0
 
steveberzinsCommented:
so, are you using any server side asp.net controls?

if you are not using the server side events stuff, maybe these pages should just be .html pages, and avoid asp.net server processing that way...
0
 
McExpCommented:
You can turn off session state, Viewstate, event Auto wireup and Request Validation which will reduce your page size, and maybe move it nearer the cleaner code you desire.
in Web.Config:
<pages maintainScrollPositionOnPostBack="false" enableSessionState="false" enableViewState="false" autoEventWireup="false" validateRequest="false" >

Unfortunatly if you have used the asp.net server controls you will probalblly find that even with viewstate off it's still there. this is due to an feature called control state. which microsoft state is to be used when you have data that must be persisted inorder for the correct opertation of the control. The only solution to getting rid of this is to not user the server controls dependant on control state.

If you have such meticulous requirements for the content of your page you are probally using the wrong technology. Microsoft have never been that good at leatting the users have ultimate control.
0
 
IllaraneAuthor Commented:
Hmm.  That line seems to have substantially improved the quality of the output, though I note that it is still rewriting my code like this:

I provide in my aspx file:
<html>
    <head>
        <title>My page title</title>
    </head>
    <body>

It produces upon viewing the page:
<html>
    <head><title>
        My page title
    </title></head>
    <body>

and similar.

It's not great, but it'll do for now until I get a chance to look at the config options and rendering engine in more detail.

Thanks. :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now