Want C# ASPX page to produce plain text rather than HTML - asp.net

I have a C# project in Visual Studio that produces an aspx page. The code I am editing is in default.asp.cs. When I build the project a file default.aspx is produced. This file looks something like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CSRValidationConnector._Default" %>
<!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">
<div>
</div>
</form>
</body>
</html>
When I make my web request most of this page comes back.
But I want the page to return only plain text and not any of this HTML. How do I configure things so that nothing is returned except what I add via Response.Write calls in default.aspx.cs?

Just remove all the HTML except:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CSRValidationConnector._Default" %>
and in your codebehind Page_Load do a Response.Write("String")

Response.ContentType is what you are looking for :)
Response.ContentType = "text/plain";

Related

ASP.NET adding a form to Master Page

So I've never really coded a website using ASP.NET. I'm trying to add a search form to a masterpage for my site. The problem is the whole body is wrapped in a form tag, which makes the functionality of my new form non-existant. I have a habbit of making bad situations worse by messing around with things I don't yet understand. So I thought I'd ask for your advice, my thoughts were:
Remove the runat="server" form tag completely.
Close it before my form and replace it at the end of my form.
Code within the button (which I later noticed you can't open.
Give up on ASP.NET and go back to PHP lol.
Hope you can help.
Thanks.
Master page will be like this
<%# Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="bodycontent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
you can write the search page like this
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="bodycontent" Runat="Server">
//dnt include form tag here having runat = server
</asp:Content>
don't add content to the master page (you can, but don't for this example), add it to the content-page (.aspx). having the form on the master page means you don't have to manually add a form to content-pages; it's there automatically. (everything on the master page will be on the content-page.)
as masood is showing, search.aspx (the content-page) is using a master page, MasterPageFile="~/MasterPage.master". content goes in the content-tag.
I realised as I am used to hard coding in open source languages, that I was using HTML specific syntax, which was conflicting with the tag surounding the whole masterpage. In using the tools on the side I found my solution.
Thank you.
Well open Visual Studio Click File -> New -> Website -> Empty Web Site.
Create a MasterPage.master and add it to you project.
Example:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<html>
//.......
</html>
Create a SearchPage.aspx and bind it with your master page. Example:
< Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MasterPage.aspx.cs" Inherits="MasterPage" Title="Untitled Page" %>

w3school asp.net syntax appears to not be asp.net?

I was looking at this page and the #{ look neat. I modified the example and made a new asp.net project. My code is below. My results were not expected. This showed up on my browser
#foreach(var row in new int[1,4,3]) { #row }
Now i'm assuming that the code on the page is asp.net and that i am doing something wrong.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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">
<div>
#foreach(var row in new int[1,4,3])
{
<tr>
<td>#row</td>
</tr>
}
</div>
</form>
</body>
</html>
This is Razor syntax, very easy and flexible used in asp.net mvc.
Visit the link
Use MVC3, search about it with Razor View Engine.
In MSVS2012, MVC is included with ultimate and express edition.
else you can take help from here
asp.net MVC4

ASP.NET - FLV player

For a company we are making 2 different sites, now one of those sites got an flv player in it and streams local flv files to the clients with the webpart from: http://www.aspnetflashvideo.com
Now on the second site we also want an flv player but this one without local files, it should stream the flv file from the other website to the users of this second website.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Assembly="ASPNetFlashVideo.NET3" Namespace="ASPNetFlashVideo" TagPrefix="ASPNetFlashVideo" %>
<!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">
<div>
<ASPNetFlashVideo:FlashVideo ID="FlashVideo1" runat="server"
VideoURL="http://flv.dumpert.nl/7649709f_YTDL_1.flv.flv" >
</ASPNetFlashVideo:FlashVideo>
</div>
</form>
</body>
</html>
Is this possible and how? Because it doesn't seem to work with the webpart.
I may be completely off track here, but it looks like your Video Url has the file extension in twice... Try:
<ASPNetFlashVideo:FlashVideo ID="FlashVideo1" runat="server"
VideoURL="http://flv.dumpert.nl/7649709f_YTDL_1.flv" >
</ASPNetFlashVideo:FlashVideo>

ASP.NET MasterPageFile causing error

I'm trying to use a master page in my website. I created a page and then created the master. I then added the tag MasterPageFile="~/master". I'm guessing that I need to do something to my main page, because I've now started getting the error:
The page contains markup that is not valid when attached to a master page
My main page is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/my.master" %>
<!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">
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I tried deleting the tag thinking that this might be the issue. Can anyone point me in the right direction?
You need to change your body to content sections. The master page works with contentplaceholders: your content pages need to provide the content for these placeholders. See the msdn page on this topic.
Quoted from that link above, your master page could contain the following:
<td><asp:contentplaceholder id="Main" runat="server" /></td>
Which the content page would fill by supplying the following
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
Main content.
</asp:Content>
Note that I included the main declaration at the top of the content page.
the pages inhering master page should have a <asp:Content as their root. this means no html tag no doctype etc.

Error with masterpage

i'm creating a brand new masterpage with VS2010 Beta 2 and I get this warning (that causes me errors in the content pages):
Validation (XHTML 1.0 Transitional):
Content is not supported outside
'script' or 'asp:content' regions.
The masterpage's code :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Bob.master.cs" Inherits="TShirtFactory.Web.All.Core.lib.masterpage.Bob" %>
<!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>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
As you can see, it's the default masterpage generated code. I get the warning when I hover the tag at top. Does anybody have an idea of what's going on ?
Thank you
In my case the error has gone after removing masterPageFile attribute from the Page-section in the web.config file:
<configuration>
<system.web>
<pages styleSheetTheme="mystyle" masterPageFile="~/myMaster.master" />
</system.web>
</configuration>
Or, if you need this attribute in the web.config file, just add empty MasterPageFile to you master page:
<%# Master Language="VB" AutoEventWireup="false" CodeFile="mySecondMaster.master.vb" Inherits="mySecondMaster" MasterPageFile="" %>
It's simple... Visual Studio is bonkers. Actually, the truth is that it can't possibly validate some markup simply because much of it is dynamic. For instance, in my project I have a constant warning about the lack of a <title> tag because it's added dynamically. Bottom line: the XHTML validator does not really know much about ASP.NET code.

Resources