Avoiding standard HTML output in ASPX page - c#

I'm sure this is RTFM, but I just can't figure out which FM I'm supposed to R.
I'm trying to serve a JNLP (Java Web Start) file (which is an XML format), and ASP.Net insists on appending HTML code to the response body.
More detail: I have a .aspx file and an accompanying .aspx.cs file. These were generated with the "new page" wizard. In Page_Load() in the .aspx.cs file, I generate some XML, do Response.ContentType = "application/x-java-jnlp-file", you know the drill.
The .aspx file, however, contains:
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs" Inherits="MyProj.MyPage" EnableSessionState="False" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
This code is appended to the output.
How do I avoid this? I tried calling Response.End() from Page_Load() but it's reportedly Evil and it throws nasty exceptions. Response.Close() is even worse, and breaks HTTP. I also tried simply removing all the HTML from the body, but ASP then complains about the fact that it needs a <head runat="server"> for something called "Themed CSS" (I'm not sure what that means).
Any leads?
Thanks!

Obligatory Use a Handler.
This gives you all the control necessary over the direct-output of information. The article included even gives an example of outputting an image.

You can turn off themes by adding EnableTheming="false" and Theme="" to Page directive
So your page would become
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs"
Inherits="MyProj.MyPage" EnableSessionState="False"
EnableTheming="false" Theme=""%>
Adding a Response.Clear() before any output should then work as expected. However Brad's comment is spot on, this is perfect for an HTTP Handler

Just have the page as :
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs" Inherits="MyProj.MyPage" EnableSessionState="False" %>
Delete the rest of the HTML in the page and as the poster suggested and do a Response.Clear()..
It is important you delete everything after the end of the
<%# Page Language="C#" CodeBehind="MyPage.aspx.cs" Inherits="MyProj.MyPage" EnableSessionState="False" %> declaration.

With a Response.Clear() or just remove it from the page!

Related

Why doesn't Visual Studio Default.aspx file let me add HTML elements?

<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XEx02Quotation._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<html>
<head runat="server">
<title></title>
</head>
<body>
</body>
</html>
</asp:Content>
I am confuse about why Visual Studio 2017 doesn't let me add HTML elements into the Default.aspx, but what I do know is that under the Solution Explorer, I see master page and apparently they are bind to the content page (which is the Default.aspx). Also, I put a copy of my screenshot problems.
So my question is do I start putting content (HTML elements) inside of that master page first!
Aside from what I have mentioned above, I also notice that when I try to put basic HTML elements into the #page directive (Default.aspx file), it says "“Content is not supported outside of 'script' or 'asp:Content' Regions” ".
The only reason I know of these terms is because I research this problem. Here are the article that I have researched. "Content Not Supposed to Be Outside 'Script' or 'asp:Content' Regions" AND https://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx
Here are the screenshot problems:
You can put HTML elements like this:
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div>Test</div> </asp:Content>.
If you want to edit the and others and things like that, you can make that inside the Site.Master.
But if you want to add other HTML pages you need to make new HTML page and add that inside the page you want(Maybe Default.aspx) , like this Stack answer.

CKEditor on ASP.NET C#

Trying to integrate CKEditor into my asp.net application....
I downloaded both CKEditor 3.x and the CKEditor for ASP.NET control and added the CKEditor reference to the site I also this to the application
<%# Page Language="C#" AutoEventWireup="true" CodeFile="CKEditor.aspx.cs" Inherits="CKEditor" %>
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CKEditor</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CKEditor:CKEditorControl ID="CKEditor1" BasePath="/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
</div>
</form>
but I am getting this error.
CS0426: The type name 'NET' does not exist in the type 'CKEditor'
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
I have searched all through the internet for solutions but it seems I am the only one having this kind of problem...please can someone help.
You have named your aspx file as CKEditor, the same name as your control assembly name having a namespace with same name, so there is a collision.
Try changing your aspx name to something else such as CKEditorTest.aspx instead of CKEditor.aspx
Its always better NOT to name your project, solution, class, control, literal, namespace etc. by any of the reserved keywords or other existing controls, classes, namespaces.
Add a reference to the CKEditor for ASP.NET Control to your website.
In Visual Studio use the Add Reference command and browse to bin\Release\CKEditor.NET.dll file from the unpacked CKEditor for ASP.NET installation package. You can also manually copy the DLL file to the bin folder of your application.
Ref: http://docs.cksource.com/CKEditor_3.x/Developers_Guide/ASP.NET/Integration_Advanced
I'am using VS 2017 and CKEditor 4.12.
First, no need to integrate this:
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
and consequently this:
<CKEditor:CKEditorControl ID="CKEditor1" BasePath="/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
You only need the following code in your web page:
<script src="../Scripts/ckeditor/ckeditor.js" ></script>
<textarea name="editor1" id="editor1" rows="10" cols="80">
</textarea>
<div >
<script>
CKEDITOR.replace( 'editor1' );
</script>

What's the difference between <head> and <asp:Content ID="HeaderContent" ...>?

This may be a newbie question, but i'm pretty new to asp.net & C# etc.
I'm working with an ASP.net website, and I'm curious about the structure of it (after automatically creating a web project), specifically the following:
I see that in Default.aspx , I have a tag like this:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>**strong text**
But in Site.master, I have this:
<head runat="server">
*etc*
</head>
So where would I put code if I wanted to include JavaScript code to run, on page load?
I believe you can put your code in any of them. The first one is for adding code or script used by all content pages(that using this master page file) while the second one is if you want to to add script or code from content pages(that should be used only for this specific page)
//in the Master page, the content here is used by all content pages
<head runat="server">
*etc*
</head>
and
//this is specific to the content page that use it. This section needs to be supplied in content pages
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
That section needs to be supplied in each content page and it will be exclusive to that page - no other page can use the script in that section
asp:Content ID="HeaderContent" is a content region. Anything within that tag will get embedded in the associated ContentPlaceHolder in the master page when it is generated.
head is a standard html markup, indicating the page head elements. Typically, the HeadContent placeholder is inside the head tag on the master page.
The head element, container for all the head elements, must use a title for the document. Some other elements it can include: style, base, link, meta, script, noscript.
The asp: Content ID = "HeaderContent" is a content element of the master page.
Have a look at the Plugging in Content part of the following link for detailed information on this: http://odetocode.com/articles/419.aspx
I think you asked when you want to use JavaScript where you put JS in your code.You can put anywhere you wish in asp side between script block such as:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</asp:Content>
or
<head runat="server">
<script type="text/javascript">
function Onclick(){
//some codes
}
</script>
</head>
Also you can put JS outside this tag. You only should use tag.

asp.net user control, getting htmlAnchor resolve to href="#"

How do you get a server control HTMLAnchor to have href="#". It keeps resolving the "#" to the control path.
<a href="#" runat="server" />
resolves to: <a href="../ControlPath/#">
I can't seem to get a google search to give me the results i want so i figured i'd ask here.
EDIT: Syntax.
Removing the runat server is not an option. It's manipulated in the backend, this was just a simplification.
I had the same problem, here's how I could resolve it:
Original code
User control:
<a id="foo" runat="server">...</a>
Code behind:
foo.Attributes.Add("href", "#");
Output:
<a id="..." href="../Shared/Controls/#">...</a>
Updated code
User control:
<asp:HyperLink id="foo" runat="server">...</asp:HyperLink>
Code behind:
foo.Attributes.Add("href", "#");
Output:
<a id="..." href="#">...</a>
I had a similar issue when rendering the page with PageParser.GetCompiledPageInstance() or when the url was rewritten. For some reason the HtmlAnchor always resolved incorrectly (similar to what you have above).
Ended up just using a HtmlGenericControl, since you are manipulating it server-side anyway this may be a possibility for you.
HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "#");
Brendan Kowitz solution will work, however i was not able to implement it due to the way this control is to operate. I ended up having to hack it as per the following code in the code behind:
lnk.Attributes.Add("href",Page.Request.Url.ToString() + "#");
Where lnk is an HtmlAnchor.
The reason for this issue in the first place is the control is not in the same directory as the page, and .Net goes about "intelligently" fixing your problem for you. The above will work, though if anyone has a better solution i'm all ears.
Originally I had this as a comment but by request I'm adding it as an answer since nobody else has explained why the original behavior is occurring or how to directly prevent it.
The URL rewriting is caused by the method ResolveURL on the Control class. I looked at it in Reflector and found that it will attempt to rewrite anything that it thinks is a relative URL if AppRelativeTemplateSourceDirectory is non-empty.
The simple workaround is to set this variable on the Page object to an empty string at some global level (or at least before Render), although this could be an issue if some other bit of your control structure requires it to be empty.
I suppose a true fix would be to get Microsoft to make UrlPath.IsRelativeUrl() smarter.
I ran into the same thing. If you set this on page load, it will work:
AppRelativeTemplateSourceDirectory = "";
Try removing the "runat" attribute and wrapping what you want to link;
<a href="#" >Your Link Text/Image Here</a>
This should work.
text
This should work.
text
Mine too works fine...I have a user control AnchorTag.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="AnchorTag.ascx.cs" Inherits="JavascriptScroll.AnchorTag" %>
<a id="A1" href="#" runat="server" >Anchor Tag</a>
And I included it as :
<%# Register src="AnchorTag.ascx" tagname="AnchorTag" tagprefix="uc1" %>
.
.
.
<uc1:AnchorTag ID="AnchorTag1" runat="server" />
.
.
And it renders as expected:
Anchor Tag
Please correct me if I'm doing something which is not expected...
EDIT: Includes nested paths
My test project renders the correct link for me:
http://localhost:2279/WebSite1/Default.aspx#
ASPX:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register src="control/WebUserControl2.ascx" tagname="WebUserControl2" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<uc1:WebUserControl2 ID="WebUserControl21" runat="server" />
</form>
</body>
</html>
Control:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs" Inherits="WebUserControl2" %>
<a id="A1" href="<%# URLHelper("~/#") %>" runat="server" >here</a>
Control Code-Behind:
protected string URLHelper(string s)
{
return Control.ResolveUrl(s);
}
What about this?
HtmlAnchor errorLink = new HtmlAnchor();
errorLink.InnerText = this.Message;
errorLink.HRef = errorLink.ResolveClientUrl("#" + this.FormControlId);
errorLink.Attributes["rel"] = "form_help";
Works for me but I'm using a Server Control in a Class Library as opposed to a User Control. I think it should work for a User Control as well.

Using nested Master Pages

I'm very new to ASP.NET, help me please understand MasterPages conception more.
I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="_SiteMaster" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="tagHead" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form id="frmMaster" runat="server">
<div>
<asp:ContentPlaceHolder ID="holderForm" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="holderFooter" runat="server">Some footer here</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and I want to use second master page for a project into sub directory, which would contains SQL query on Page_Load for logging (it isn't necessary for whole site).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<asp:ContentPlaceHolder ID="holderForm" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooter" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
But I have a problem: footer isn't displayed.
Where is my mistake? Am I right to use second master page as super class for logging?
Project page looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="Server">
Some footer content
</asp:Content>
I've been working with nested master pages and have run in to something similar. From what I see where you have "Some footer here" in the Site.Master is where the problem lies and I've had similar problems with having content with-in a contentplaceholder tag. if you try this instead
<asp:ContentPlaceHolder ID="holderFooter" runat="server"/>Some footer here
Then you should be able to see the footer content.
I'm not sure I'd use master pages for this. If it's really just going to do logging, I'd implement IHttpModule, register it in web.config, and then check whether or not to log based on the path of the request. I think of master pages as being about content rather than other processing such as logging.
See the IHttpModule walkthrough on MSDN for an example - in your BeginRequest handler, you'd probably check the request path and log appropriately if it matched.
Apologies if I misunderstood what you're trying to do though.
You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page...
When you move the "Some footer here" text to your Content, you will see your lines of text :)
HTH
This link gives a simple explanation on Master pages,
http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/
The question are you right to use child Master pages in this instance - I would say master pages should be helping you solve issues around building a consistent layout, not for whether or not logging should occur.
The problem is, when the text elements placed inside Default.aspx are put in their relative Content Placeholders, they are written on the placeholders of your Site.master page and not those of Project.master (which have the same names).
You should resolve the naming conflict, by assigning different ContentPlaceHolderIDs to the the placeholders in Project.master (this means you'll also have to change the references in Default.aspx).
This would be your Project.master file:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<!-- whatever... -->
<asp:ContentPlaceHolder ID="holderFormInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
<!-- ... -->
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooterInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
And thus, your .aspx pages that use the Project master page instead of the global Page.master must be changed to:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderFormInternal" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooterInternal" runat="Server">
</asp:Content>
If the only reason is to implement loggin why would you mess around with masterpages?
If the logging isent supposed to display any text!?
You either do as Skeet proposed with an IHTTP handler.. Or lazier one would be do have a class that derives from webpage and implement logging in that class and make your pages that need logging dervice from that..
ex:
public class LoggingPage : : System.Web.UI.Page
{
public override void OnLoad()
{
// Do logging
}
}
partial class OneOfTheWebPages : LoggingPage
{
public void onLoad()
{
base.onLoad();
}
}
I may be misunderstanding your problem - but from the code you've posted, there isn't anything in the footer.
In your Project page, the <asp:Content> tag for the holderFooter content place holder doesn't have anything in it.
I have next inheritance tree:
Site.master <-- Page1.aspx
<-- Project.master <-- Page2.aspx
And I don't know why Page2 display only content of itself and it's master page - Project. But doesn't display a content of Site (as Page1 does) Why? What have I to write for doing that?

Categories

Resources