Response.Redirect and HTML content - asp.net

I have a Web Form whose sole purpose is to redirect to other pages. I created it as a normal aspx page and then I deleted everything in the .aspx file and kept just the first line shown below--even the Doctype and HTML tag are gone now:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Redirect.aspx.cs" Inherits="Web.Redirect" %>
I also deleted the .designer.cs file as it contained nothing. It works, but I wonder if what I did is right. Are there any concerns about removing all HTML content from the Web Form in this case?

None whatsoever. What you have done is perfectly acceptable.
However, if the sole purpose of the pages is to redirect, I would use a Handler/ASHX file as it can be used in exactly the same way and doesn't have as much overhead as the ASPX page.
Here is a description and example of how to use one.

If you do Response.Redirect(url), a redirect header is added and the request is ended. This means that anything in your ASPX page is not output to the client. Any content after Response.Redirect(url) is not output to the page. You can just as well delete it, like you did.
If you do Response.Redirect(url, false), the response is not ended and your page is output to the client. However, the client never gets to see it because he is redirected.

Related

Only Content controls are allowed directly in a content page that contains Content controls in ASP.NET

I have an application which has a master page and child pages. My application is working fine on local host (on my intranet). But as soon as I put it on a server that is on the internet, I get the error shown below after clicking on any menus.
Only Content controls are allowed directly in a content page that contains Content controls.
Double and triple check your opening and closing Content tags throughout your child pages.
Confirm that they
are in existence
are spelled correctly
have an ID
have runat="server"
have the correct ContentPlaceHolderID
I had exactly the same issue. The problem was I had some spaces after the ending content tag:
</asp:Content>
Remove all the spaces, line breaks after the last closing tag.
I was facing a similar issue. Are you surrounding your code with the "content" tag ?
<asp:Content>Add your HTML here</asp:Content>
And have separate content tags for your sections .
A head content for the header declaration and a body content for the body declaration .
Another possibilty is line endings. I copied an older version of code from source control which enforced Unix style line endings. Since it wasn't a checkout, it didn't automatically convert the line endings to the DOS/Windows style. The error message was the "Only Content controls are allowed directly ..." error even though the page was layed out properly. It appears that the lack of Windows style line breaks caused the ASPX parser to fail.
I was able to fix it by pasting the code into a line ending agnostic editor (which caused the line endings to be normalized to the Windows style), re-copying it to the clipboard and pasting it back in Visual Studio, after which the page processed without errors.
In the case presented by Tripati Subudhi in the question, it's entirely possible that something about the deploy process used inadvertently converted line endings to the Unix style, leading to the error.
Another possible issue is HTML comments, I had these surrounding a Content control - I believe ASP.NET converts these into literal controls behind the scenes - hence the error i
another potential cause for this error: tags with the wrong case.
changing <asp:content>... to <asp:Content>... fixed the issue in my case.
the reason for the faulty case was the built-in format document function in visual studio 2012(and 2013) with default settings.
the setting for this can be changed in Tools->Options->Text Editor->HTML (Web Forms)->Formatting: set the tag capitalization to 'as entered' and studio will no longer destroy your files.
Check your document for non-printing characters
My masterpage contained two UTF-8 BOMs right at the start of the file because I pasted the <%# Master %> directive from another page. I was able to make it work by backspacing them out.
For me, it didn't like that I had an Assembly and a Page directive commented out:
<%--<%# Assembly Name="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %>--%>
<%--<%# Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyClass.MyPage" MasterPageFile="~/_layouts/MyProject/MasterPages/MasterPage.master" %>-->
Even though I had a valid Page directive after that, and was not using System.Core for anything. After just removing them, it loaded fine.
For me it was two content controls that had the same ID - the file had been edited outside of Visual Studio, so the auto-rename of duplicate ID didn't happen. This misleading error was highlighting the first image inside the second content control with the same ID as the first - what a wild goose chase!
Copying the entire page and reposting it over itself solved it, because VS at that point then renamed the duplicate control ID.
In my case I forgot to add assembly reference of AjaxControlToolkit.dll.
When I add the reference the error disappeared.
in SharePoint it happened since a pageLayout wasn't published.
I had a silly syntax error I kept overlooking. There was an extra < at the start of my MasterType tag I couldn't see for the life of me 🤦‍♂️.
<%# Page Language="vb" AutoEventWireup="true" MasterPageFile="~/Site1.Master" CodeBehind="Default.aspx.vb" Inherits="SomeApp.Web._Default" %>
<<%# MasterType VirtualPath="~/Site1.Master" %>

Is it a good idea to use plain HTML instead of ASPX

I'm developing an ASP.NET website. Sometimes some modules contain so few content that I present them inside a jQUeryUI dialog and communicate with the server via AJAX. I keep those contents inside a separate file and load them to a jQueryUI dialog according to the appropriate module.I was wondering if it's a good idea to have that content as a plain HTML elements instead of asp ones. Somehow I thought maybe this would reduce the overhead the conversion from asp elements to html elements can cause.
I'd allways go with the aspx Page, because a dynamic Page is more work at the beginning but in the end it almost ever saves time.
Specially when your not sure of the content that will be shown there, it is better.
And for the one reason i do it, is to have everything the same.
One style one way to code.
I'd say this is probably premature optimization. The overhead of an aspx page is in almost all cases negligible. I believe it's more likely that you will some day need to put dynamic things in that page, in which case you would have to convert the html file to an aspx, and change the url for your ajax dialog - which will cost time/money.
If you have aspx pages, or ascs user controls that you do not actually use/run any code, you can set the AutoEventWireup the EnableViewState, and maybe the EnableSessionState to false and stop the calling of the PageLoad and the rest functions and reduce the overhead. So on top of the controls you declare:
<%# Control AutoEventWireup="false" EnableViewState="false" ...
or for page:
<%# Page AutoEventWireup="false" EnableViewState="false" EnableSessionState="false" ...
The disable of the session is let the pages loads in parallel, the disable of the EnableViewState is reduce the size, the AutoEventWireup is reduce the callback hooks and calls.
In general you can use what ever you wish - if your pages can work, but if you like to keep it robust and easy to change or update, or add new functionality in the future, then use dynamic aspx pages.
Similar question: Master page and performance

ASP.NET Include Disables Code-Behind

I've found that when using the
<!-- include file="MyPage.aspx" -->
command in ASP, I'm not able to use the code-behind in MyPage.aspx.
The issue is that when I try to include MyPage.aspx, there is an error because we have two Page Directives. If I remove the Page Directive, I can include MyPage.aspx just fine, but cannot access the code-behind, because the "CodeBehind" parameter in the Page Directive is no longer there.
So, as far as I can tell, we have a Catch-22. Does anyone know of a work-around for this? Or is there just something I'm missing?
Thanks,
-Onion-Knight
I'm not sure if this changes anything, but I am using a Master Page with the page that includes MyPage.aspx.
Why don't you use a user control (*.ascx) instead of including an aspx page?
Have a look at this overview in MSDN which shows how to create and user "user controls".

Page_Load Is not Hit in SubFolder Default.aspx after Redirect to that Page

I have a page that sites in a first level folder from my root called default.aspx.
I'm redirected to that page like as so:
I have an initial root Default.aspx that holds a login button
User clicks the login button and it redirects to Facebook Login
After logging in, Facebook redirects back to my first level Default.aspx (sits inside root\firstlevelfolder\Default.aspx)
The page_load is not being hit. This is a .NET 2.0 solution in VS 2008. The AutoEventWireup is set to true in the page directive.
Not sure why and have not seen this error before. Does it have something to do with redirecting to a non root-level .aspx page? This is probably something fundamental but I"m not sure what it is.
breakpoint in the first line of code
in my page_load
You're using a breakpoint. Double-check that Visual Studio is in debug mode and your browser is pointing to localhost:1234 (where 1234 is some random port number assigned by the VS web server). Also, try doing a simple Response.Write("Hello World") in Page_Load(), and see if anything prints out on the top of the page.
You said to try the trace in the root.
No. Try it in the page that you think that Page_Load isn't firing on.
Also, on StackOverflow, it is much easier for others to follow the conversation, and it makes for a cleaner post if you can reply to questions using the "add comment" feature. The reason I'm posting another answer is just in case you're not aware of the feature and you miss the comment. See the SO Community FAQ item regarding comments.
Put a Trace="True" in your <%# Page %> line in the "first level" Default.aspx. The part that you would want to look at is the second section - "Trace Information". See if there is a "Begin Load" item in that list.
Also, is your page inheriting from the System.Web.UI.Page object, or do you have a custome base page?
public partial class _Default : System.Web.UI.Page //or do you have a different object here?
{
...
}
Does Page_Load get hit if AutoEventWireup is false?
Does the page that you are redirecting to work if you hit it directly from your browser without an intermediate page that redirects you there? I don't think the cause of your problem has to do with the fact that the page is in a subdirectory.
If the trace is showing that Begin Load is firing, then what methodology are you using to determine that Page Load doesn't work?

Access Controls which are written by Response.WriteFile("Sample.htm")

My aim is to stream a .htm file via Response.WriteFile("Sample.htm"); and then access a specific html element (ex. <a runat="server" id="myAnchor" /> ) from the Response which happened in the Page_PreInit Event.
I tried it already with ((HtmlGenericControl)myAnchor) but it doesn't work. It only works, if the anchor tag is inside the .aspx page.
Is there a possibility to reinitialize the .aspx page after the response.write event happened, so that the anchor tag from the sample.htm file gets indexed like it would be an anchor tag from the .aspx page.
Thanks for your help.
No. Once you write anything directly to the response stream it leaves the web server (where your code is running) and goes directly to the browser. Do not pass 'GO'. Do not collect $200.
Anything in that file is never loaded into your Page class' control tree in the first place, but sent directly to the browser. "reinitialize the .aspx page" wouldn't help you. Instead, to re-use content like this, you need to embed it in a control that can be included on the page or put it in a master page.

Resources