Declaration Expected Error - asp.net

I am trying to read an rss feed in asp.net. I found the follow code to get me started. the line:
WebRequest request=WebRequest.Create("myfeedurl");
Returns the error "Declaration Expected." I can't for the life of me figure out what could be wrong or what Declaration Expected means. Any help would be greatly appreciated I posted everything I have in the section below.
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%# Import Namespace="System.Xml" %>
<%# Import Namespace="System.IO" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script runat="server">
WebRequest request=WebRequest.Create("myfeedurl");
WebRequest response=request.GetResponse();
Stream rssStream=response.GetResponseStream();
XMLDocument xmlDoc=new XMLDocument();
xmlDoc.Load(rssStream);
</script>
</body>
</html>

You are trying C# code in VB.Net.
Your code should be:
Dim request As WebRequest = WebRequest.Create("myfeedurl")

Related

ASP.NET Web Forms call embedded VB Code from another aspx. page

I got a Project where somehow an Method is called from another aspx.page. I want like to know how this work.
For instance, in my Foo.aspx I got this:
<script runat="server">
Sub ShowHint()
some code
End Sub
</script>
in Bar.aspx I got this:
<script runat="server">
ShowHint()
</script>
But how does this can even work? I dont get it.
You can use JavaScript (AJAX) to get the data from a different page.
It is easy to do with the jQuery Load function.
You can also define classes where you can define subs of function which you can call from every webpage.
Is the webpage inside of you project?
An easy example: I have a file test.aspx and I want to load some data from test2.apsx. To load the data I use jQuery.
Here is test.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
//$("#presenter").load("test2.aspx" ... = loads the content of test2.aspx into the div with id = presenter
//$("#presenter").load("test2.aspx #content" ... = loads onlay the content of the div with id = content from text2.aspx
//{ message01: "Hello", message02: "world" } = are the paramter I pass to test2.aspx
$("#presenter").load("test2.aspx #content", { message01: "Hello", message02: "world" }, function () {
//here you can place code which will run after the load is completet
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="presenter"></div>
</form>
</body>
</html>
Here is test2.aspx
<%# Page Language="VB" AutoEventWireup="false" CodeFile="test2.aspx.vb" Inherits="test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="content" runat="server">
</div>
</form>
And the code from test2.asp
Partial Class test2
Inherits System.Web.UI.Page
Private Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
Dim msg01 As String = Request("message01")
Dim msg02 As String = Request("message02")
Me.content.InnerHtml = msg01 & " " & msg02
End Sub
End Class
Ok I got this. Just have to include my "Helper" aspx-file in to another aspx-File:
<!--#include virtual="somefilename"-->

ASP.NET MVC Master Page Variable Scope

I don't understand the scope of the variables in my .Master pages - can anyone assist?
In the example below settings cannot be seen in either usage following its instantiation:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<% var settings = new SettingRepository(); %>
<!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">
<meta name="description" content="<%: settings.metaDescription %>"/>
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" /> - <%: settings.pageTitle %>
</title>
...
In the example below settings can only be seen in the FIRST usage, but not the second:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!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">
<% var settings = new SettingRepository(); %>
<meta name="description" content="<%: settings.metaDescription %>"/>
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" /> - <%: settings.pageTitle %>
</title>
...
According to me what ever variable you use in Master page it is accessible to its XAML file and cs file.
You can access it in its content pages like this:
String variable=((MasterPageName).this.Master).variable;
Use #MasterType directive, as explained here:
http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

BC30451: 'NewsBody' is not declared. It may be inaccessible due to its protection level

Try as i might i cant get past this error. I'm not an ASP.Net developer but due to reasons outside my control have to develop 2-3 pages in ASP.
I'm using Dreamweaver and have the asp.net set to compile at first load.
.aspx File
<%# Page Language="vb" AutoEventWireup="false" Src="Scripts/CurrentNews.aspx.vb" Inherits="NewsFunctionality" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>News</title>
<link rel="stylesheet" type="text/css" href="CSS/NewsLayoutOne.css"/>
</head>
<body>
<div class="OuterDiv" runat="server">
<img id="NewsImage" src="Images/DefaultNews1.png" >
<div id="NewsBody" runat="server">Original Text</div>
</div>
</body>
</html>
.aspv.vb file
' VB Document
public partial Class NewsFunctionality
Inherits System.Web.UI.Page
'On load event for the page linked to this class file
protected sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
NewsBody.InnerHTML = "<p>Dynamic Text Generated.</p>"
End Sub
End Class
Check that 'NewsBody' have been declared within the designer.vb file.
It could look something like this since you are using a div:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl NewsBody;

Thread.Sleep in aspx error page. Why?

In this advisory concerning the oracle padding exploit, Microsoft posted the following recommended error page:
<%# Page Language="C#" AutoEventWireup="true" %>
<%# Import Namespace="System.Security.Cryptography" %>
<%# Import Namespace="System.Threading" %>
<script runat="server">
void Page_Load() {
byte[] delay = new byte[1];
RandomNumberGenerator prng = new RNGCryptoServiceProvider();
prng.GetBytes(delay);
Thread.Sleep((int)delay[0]);
IDisposable disposable = prng as IDisposable;
if (disposable != null) { disposable.Dispose(); }
}
</script>
<!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>
<div>
An error occurred while processing your request.
</div>
</body>
</html>
What's with the Thread.Sleep for some value between 0-255? I don't want my server threads tied up for up to quarter of a second.
The reason is to alter the timing of the result. By making the return take a variable amount of time, you can't use the timing of the error return to determine the reason for failure, which is the approach that is used for the attack

ASP.NET inline coding: variable name is not replaced with value

I have an ASP.NET page .In the page load i set the value of a public variable.and in the inline coding part,I Am loading a CSS which is the folder with the name which is available in the public variable.My HTML markup is as follows
<%# Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Theme="GridView" Inherits="GUI.MyPage" %>
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>MyPage</title>
<link href="../Vendors/<%=vendorName%>/css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%=vendorName %> <!-- here value is printed correctly -->
...
</body>
and in my code behind
public partial class MyPage: MyCommonClass
{
public string vendorName = "";
protected void Page_Load(object sender, EventArgs e)
{
vendorName = "ACLL";
}
}
But when i run the page, the <%=VEndorId%> is not replaced with the value in it .But in the Body,It is printing properly.But in the head it is not coming.I checked the ViewSource and find the source HTML as follows
<link href="../Vendors/<%=vendorName%>/Lib/css/tradein.css" rel="stylesheet" type="text/css" />
The two options are:
<link href="<%= string.Format("../Vendors/{0}/css/style.css", vendorName) %>" type="text/css" rel="Stylesheet" /> // as Greco stated
and
<style>
#import url("../Vendors/<%=vendorName%>/css/style.css");
</style>
Add the runat="server" tag to the link element.
Similar question and good answer here.
The solution is to move the quotes around the inline code into the code
<link href=<%="'../Vendors/" + vendorName + "/css/style.css'"%> rel="stylesheet"...
^ ^

Resources