VB.NET 2.0: Where does a URL in code come from? - asp.net

I have to debug an old VB.NET 2.0 code of someone who has left the company. We have a production system (let us call it http://prod) and a test system (http://test). Both are nearly similiar including a documents repository. When looking at docs in production, all the hyperlinks showing up at the bottom are okay (meaning they say something like http://prod/download.ashx?id={GUID}).
However in test it is the same (http://prod/download.ashx?id={GUID}), even it should be http://test/download.ashx?id={GUID} instead.
After hours of debugging I have found the relevant line of code:
html += "<td><a href='" + HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "/") + "int/download.ashx?id=" + row.Item(0).ToString() + "' target='_blank' class='" + row.Item(3).ToString() + "'>" + row.Item(1).ToString() + "</a>" + privat + "</td><td>" + row.Item(2).ToString() + "</td>"
Looking at html this shows i.e.
"<table class='table_dataTable'><thead><tr><td>Name</td><td>Jahr</td></tr></thead><tbody><tr><td><a href='http://prod/int/download.ashx?id=4d280886-db88-4b25-98d8-cf95a685d4a4' target='_blank' class='doc'>Document for managers</a></td><td>2014</td>"
So I wonder, where does this come from incorrectly? I may have found the relevant part of coding, but I am not sure, what to do now, respectively if I am right on this?:
Public Class download : Implements IHttpHandler, IReadOnlySessionState
Dim debug As String = ""
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim fehler As String = ""
Try
' Get the file name from the query string
Dim queryFile As String = context.Request.QueryString("id")
debug += "id=" + queryFile + "<br>"
Any help is appreciated as VB.NET is not my main focus.

You have probably checked this but sometimes the obvious gets overlooked.
Verify the URL in your browser window. Make sure it has not changed to http://prod... while you were navegating.
Verify that your web application is not using frames. The page in question could be loaded in a frame using the prod URL. If this is the case your web.config might have a setting to say where this frame is loaded from or it might simply be hardcoded.
Check for URL Rewrite rules in IIS or your web.config

Related

How to get package name (Source Name) from the event log for SSIS errors in ASP.NET

I’m successfully reading the server event log to display SSIS errors from the Application event log. The aim is to make it possible to do first line troubleshooting without having to log into the server as an Administrator.
I’m using the EventLogEntry in vb.net to show Errors where the source is SQLISPackage110. However what’s stumping me is that when viewing the errors in the event log there is a “Source Name” property that displays the package name. However the EventLogEntry doesn’t seem to have this property which makes it impossible to tell which package has returned the error.
Has anyone come across this and managed to find a way round it?
A screenshot of the error and some of my vb.net code is below
Dim myEventLogEntryCollection As EventLogEntryCollection = myEventLog1.Entries
myEventLog1.Close()
Dim x As Integer
Dim entry As EventLogEntry
If myEventLogEntryCollection.Count > 0 Then
strTable += "<table class='table table-bordered'><tr><th>Time Written</th><th>Source</th><th>Event type</th><th>Message</th></tr>"
For Each entry In myEventLogEntryCollection.Cast(Of EventLogEntry).Reverse 'cast and reverse to show newest first
If entry.Source = strEventSource And (entry.EntryType.ToString = "Error" Or entry.EntryType.ToString = "Warning") Then
strTable += "<tr>"
strTable += "<td>" + entry.TimeWritten.ToShortDateString + " " + entry.TimeWritten.ToShortTimeString + "</td>"
strTable += "<td>" + entry.Source.ToString + "</td>"
strTable += "<td>" + entry.EntryType.ToString + "</td>"
strTable += "<td>" + entry.Message + "</td>"
strTable += "</tr>"
x += 1
End If
If x > 100 Then Exit For 'could be 1000s in the log
Next
strTable += "</table>"
End If
As you've noticed, "Source" is NOT one of the LogEntry properties.
Instead the word "Source" is just text trapped inside of the larger Message property. So, to get the "Source" information you're going to have to parse it out of the Message. Split the message into lines. Then loop over the lines and do something when you find a line containing the word "Source"
This code is NOT tested. But, just to give the idea..
Dim messageLines As String() = Nothing
messageLines = entry.Message.Split(Environment.NewLine)
Dim line As String
For Each line In messageLines
If line.Contains("Source") Then
'your code here
Next line
SSIS packages can log to many destinations. And yes, logging to the Windows Event log is an option, but I've seldom seen it used in practice. Windows Event Logs aren't permanent. By default, they are going to start trimming the history once they get over twenty megabytes in size. They are a bad place to store SSIS log information for troubleshooting.
Would you consider gently suggesting to your package authors that they use SSIS SQL logging instead? If they logged to SQL then you'd have the information you need arranged, and stored in a nice searchable manner that would allow for easier querying, aggregation, and analysis across your entire organization. You'd even have the source column you could use to get the exact info you need.

Detecting current page when on root page ASP.NET with VB

my current code is below. If you visit the root of my webpage http://evanparsons.net/ you will notice that the current page is not highlighted, however when you visit a page, it works. For testing purposes, I had it return the "myPage" value to see why it isn't working... it still returns index.aspx.
Dim pageName As String = System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.Url.AbsolutePath)
While DBReader.Read()
_link = _link + "<li><a href='" + (DBReader("source")) + "'"
If ((pageName) = (DBReader("source"))) Then
_link = _link + "class='current' "
End If
_link = _link + pageName
_link = _link + ">-" + (DBReader("name")) + "- </a></li>"
End While
Basically, my navigation comes from a master page that scans my database, and as it cycles through it, I want to add a css class called current.
Have you tried converting them into lower case?
If (pageName.ToLower() = (DBReader("source")).ToLower()) Then

ASP.NET redirect not working with invalid URL with UIProcess application block - looking for redirect architecture ideas?

In my global.asax.vb file, I have code to re-write the URL if there is a prefix on the URL. We are introducing a new context in our application. So every page will either be of context hair or saliva.
Before the ASP.NET code (stack) even reaches this Global code, it calls an application block called UIProcess. It's code that Microsoft wrote years ago, and is no longer supported. The UIP block sort of mimics MVC, and you store all views, navigation and controller details inside the web.config. The UIP block is doing a redirect as shown below. Note, they had a known bug that was never fixed (commented out), so I had to recompile it before upgrading from .NET 2.0 to .NET 3.5. That's what I have commented out. That's the only bug I'm aware of.
private void RedirectToNextView(string previousView, ViewSettings viewSettings)
{
try
{
//if (previousView == null)
// HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/" + viewSettings.Type, true);
//else
// HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/" + viewSettings.Type, false);
if (previousView == null)
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + viewSettings.Type, true);
else
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + viewSettings.Type, false);
}
catch (System.Threading.ThreadAbortException) { }
}
Here is the Global.asax.vb code:
(again this code doesn't matter right now because it's not getting here YET with the exception being thrown)
Sub Application_BeginRequest(ByVal sender As Object, _
ByVal e As EventArgs)
' Fires at the beginning of each request
Dim originalUri As Uri = Request.Url
Dim rewrittenUrl As String = String.Empty
'Rewrite Saliva and Hair Testing urls
Select Case True
Case originalUri.AbsolutePath.StartsWith("/HairTest/")
rewrittenUrl = originalUri.AbsolutePath.Remove(0, 9)
If Not originalUri.Query.Contains("SampleTypeContext=247") Then
rewrittenUrl += "?sampleTypeContext=247"
End If
Case originalUri.AbsolutePath.StartsWith("/SalivaTest/")
rewrittenUrl = originalUri.AbsolutePath.Remove(0, 11)
If Not originalUri.Query.Contains("SampleTypeContext=3301") Then
rewrittenUrl += "?sampleTypeContext=3301"
End If
End Select
If rewrittenUrl <> String.Empty Then
'append the original query if there was one specified
If originalUri.Query <> String.Empty Then
If rewrittenUrl.Contains("?") Then
rewrittenUrl += "&"
Else
rewrittenUrl += "?"
End If
rewrittenUrl += originalUri.Query.Remove(0, 1)
End If
Context.RewritePath(rewrittenUrl)
End If
End Sub
The application is actually causing an exception above, when I try to pre-pend my URL (viewSettings.Type variable above) with "/HairTest" or "/SalivaTest". It causes that System.Threading.ThreadAbortException. I'm thinking because that path doesn't actually exist in our web application, but I'm just guessing. Notice, we're doing a re-write in our global, not a redirect. Our re-write prepends the URL with "/HairTest" or "/SalivaTest".
All of the pages in our web application expect that "SampleTypeContext" parameter if it needs it. If you can think of a way that will work better for this situation, let me know. I'll try to get more details on the exception.
Looking for ideas!! Our architecture approach is still up for discussion if we run into issues with this UIProcess block. We can't just get rid of the UIP block since it's used throughout our application, but I can modify the code above (in my first code snippet) if we need to.
There's no standard way to do this with the UIP block. And Microsoft doesn't maintain it anymore. I overloaded a bunch of the methods in the library to make it work. So that we can append a query string parameter (applicationScope) on each of our UIP redirect calls.
If you need the code, add a comment here, and I'll send you the source.

ASPX to ASP Conversion (E-mail Script)?

I am a PHP Developer and I do not have any knowledge about ASP. Sadly I am the only developer here and a client had ASP (not ASP.NET) pages and they wanted a "Contact Us" Form.
I searched the internet to find out how to serve ASP files in Apache and it pointed me to mod_aspdotnet. It was so stupid of me to think that ASP is the same as ASP.NET! Now do you see my problem? I have developed a working ASP.NET email script. The files had .aspx extensions but the actual site had .asp extensions, they were using ASP and not ASP.NET!
So I would like to ask for help. This is my email script:
<%# Page Language="VB" Debug="true" %>
<%#Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub Send2Mail (sender as Object, e as EventArgs)
Dim objMail as New MailMessage()
if Logo.HasFile Then
Try
Logo.SaveAs(Server.MapPath("uploads/") + Request.form("strName") + "_Logo_" + Logo.FileName)
objMail.Attachments.Add(new MailAttachment(Server.MapPath("uploads/" + Request.form("strName") + "_Logo_" + Logo.FileName)))
Catch ex As Exception
msg.Text = "<b>The Logo could not be uploaded</b>. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
end try
end if
if Image1.HasFile Then
Try
Image1.SaveAs(Server.MapPath("uploads/") + Request.form("strName") + "_Image1_" + Image1.FileName)
objMail.Attachments.Add(new MailAttachment(Server.MapPath("uploads/" + Request.form("strName") + "_Image1_" + Image1.FileName)))
Catch ex As Exception
msg.Text = "Image 1 could not be uploaded. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
end try
end if
if Image2.HasFile Then
Try
Image2.SaveAs(Server.MapPath("uploads/") + Request.form("strName") + "_Image2_" + Image2.FileName)
objMail.Attachments.Add(new MailAttachment(Server.MapPath("uploads/" + Request.form("strName") + "_Image2_" + Image2.FileName)))
Catch ex As Exception
msg.Text = "Image 2 could not be uploaded. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
end try
end if
objMail.To = "example#example.com"
objMail.From = """Us"" <do-not-reply#foo.com>"
objMail.BodyFormat = MailFormat.Html
objMail.Priority = MailPriority.Normal
objMail.Subject = "Business Registration"
objMail.Body = "<html><body style='font-family: Verdana'><table style='font-family: Verdana; font-size: 11px'>"
objMail.Body += "<tr><td><b>Business Name:</b></td><td>" + Request.form("strName") + "</td></tr>"
objMail.Body += "<tr><td><b>Opening Business Description:</b></td><td>" + Request.form("strOpenDesc") + "</td></tr>"
objMail.Body += "<tr><td><b>Opening Hours:</b></td><td>" + Request.form("strHours") + "</td></tr>"
objMail.Body += "<tr><td><b>Business Description:</b></td><td>" + Request.form("strBusDesc") + "</td></tr>"
objMail.Body += "<tr><td><b>Servicing Area:</b></td><td>" + Request.form("strService") + "</td></tr>"
objMail.Body += "<tr><td><b>Website Address:</b></td><td>" + Request.form("strWebsite") + "</td></tr>"
objMail.Body += "<tr><td><b>Email Address:</b></td><td>" + Request.form("strEmail") + "</td></tr>"
objMail.Body += "<tr><td><b>Telephone Number:</b></td><td>" + Request.form("strPhone") + "</td></tr>"
objMail.Body += "<tr><td><b>Fax Number:</b></td><td>" + Request.form("strFax") + "</td></tr>"
objMail.Body += "<tr><td><b>Mobile Phone Number:</b></td><td>" + Request.form("strMobile") + "</td></tr>"
objMail.Body += "<tr><td><b>Suburb / Post Code:</b></td><td>" + Request.form("strPostCode") + "</td></tr>"
objMail.Body += "<tr><td><b>Proprietor Name:</b></td><td>" + Request.form("strPropName") + "</td></tr>"
objMail.Body += "<tr><td><br /></td></tr>"
objMail.Body += "<tr><td><b>Image 1 Caption:</b></td><td>" + Request.form("strImage1Caption") + "</td></tr>"
objMail.Body += "<tr><td><b>Image 2 Caption:</b></td><td>" + Request.form("strImage2Caption") + "</td></tr>"
objMail.Body += "</table><body></html>"
SmtpMail.SmtpServer = "localhost"
Try
SmtpMail.Send(objMail)
strMessage.Visible = true
Catch ex As Exception
msg.Text = "<b>The message was not sent</b>. The following error occured: <i>" + ex.Message + "</i><br />"
msg.Visible = true
End Try
End Sub
Will this work in ASP as it is? What do I need to change to make it work in ASP? I am also using the <asp:></asp:> tags. Will this work in ASP?
EDIT
I am sorry for the confusion of what server I am using for development and the host server. The host is using IIS. I am using Apache because I am really a PHP developer. The problem about running ASP pages in Apache was because the client does not want to give me access to their server. They told me that I should just deliver the files.
ASP and ASP.NET share names for pure marketing reasons but they are basically unrelated technologies, although the latter reuses concepts (such as being a language agnostic framework) and even some method names (such as Server.MapPath).
In both ASP's, you have to pick a supported programming language and code your scripts with it. Your sample code seems to use VisualBasic, which was not supported by classic ASP. You'll have to switch to (e.g.) VBScript. Unlike VisualBasic, VBScript is loosely typed, but apart from that it has a similar syntax.
The classic ASP way to send e-mail is the CDONTS library. A simple example:
<%
set mailer = CreateObject("CDONTS.NewMail")
mailer.from = "foo#example.com"
mailer.to = "bar#example.com"
mailer.subject = "Test e-mail message"
mailer.body = "This is the message body."
mailer.send
set mailer = nothing
%>
In general, classic ASP was very similar to PHP: you embed code in your HTML documents.
The difficult part is file upload handling. ASP did not have a native file upload feature: you had to purchase and install a binary commercial library or find one of the VBScript-only code snippets available at the Internet.
Edit: Does your client really run ASP over Apache? Microsoft only supports IIS. Apache modules for ASP were normally written by third-parties and often implemented different languages like Perl.
ASP (or Classic ASP as it is now known) and ASP.NET are different beasts. The first is a scripted language similar to PHP, while ASP.NET use the .NET Framework and is compiled to a MSIL binary either before being run and uploaded as a .NET assembly, or on the fly by IIS if the page contains inline script, either way they are then run by the CLR (common language runtime) which forms the core part of the .NET framework.
While some VB.NET code could be converted back to VBScript (the most commonly used Classic ASP language), anything that uses any part of the .NET framework outside of the Microsoft.VisualBasic namespace won't be available. To access this kind of functionality you would need to use third party ActiveX/COM components.
You say you want to serve up the ASP script on Apache, is that because you don't have access to an IIS server or that this site that is already running in on Apache? As far as I am aware the only way to run Classic ASP on Apache is to use ChilliSoft/SunOne ASP for Apache. Some hosting companies offer this, but it's a legacy product.
If it is running ChilliSoft ASP then there is a mail component that could be installed, if the host has installed the SpicePack. Take a look at this for more information:
http://developers.sun.com/asp/howto/chilimail.html
http://ns7.webmasters.com/caspdoc/pdf/chilisoft_asp_docs.pdf
I've ported numerous ASP applications from IIS to Apache using ChilliSoft ASP and generally it works very well.
Have a look at this answer for one way on how to send email using ASP: Sending Mail code in ASP
Failing everything else, just add yourself a page to send the mail in PHP and get the contact form to POST to that page, and then redirect back to an ASP page if you want it to appear seamless.

sHow to determine the path of the current web site

I wanted to create a function which would return the path of the current web site. This is what I thought was working while running in the IDE:
Public Shared Function WebsiteAbsoluteBaseUrl() As String
Dim RequestObject As System.Web.HttpRequest = HttpContext.Current.Request
Return "http://" & RequestObject.Url.Host & ":" & _
RequestObject.Url.Port & "/" & _
RequestObject.Url.Segments(1)
End Function
this seem like it should work? Is there a more straight forward way?
I am trying to replace all occurrences of the tag "{WebSiteBaseUrl} in an html email with the absolute web site path.
<A HREF="{WebSiteBaseUrl}Files/filename.pdf/>Some Text</A>
I don't want to have to parse the template and try to make sense of it in order to use MapPath function on a relative path to a file name.
The resulting email will become the html body of an html email, so I need the links to be absolute.
Some Text
I guess what I am seeing in dev, running in the IDE, is that "RequestObject.Url.Segments(1)" returns the name of the virtual folder and that in production it seems to be returning the name of the web page. I guess this probably has to do with how the web site is set up in IIS.
What does this suggest about how the web site is set up in production and how would you suggest I modify the code to yield a correct path after replacing {WebSiteBaseUrl} with the result of the function? This assumes that the file "filename.pdf" is in the relativepath ~/files/filesname.pdf.
You can use Request.Url.AbsoluteUri for this. Server.MapPath(".") will give you physical path.
Also you can use other Request properties which are listed here.
Try this to get the website's application path:
Public shared ReadOnly Property FullyQualifiedApplicationPath() As String
Get
Dim appPath As String = Nothing
Dim context As HttpContext = HttpContext.Current
If Not context Is Nothing Then
'Formatting the fully qualified website url/name
appPath = String.Format("{0}://{1}{2}{3}", _
context.Request.Url.Scheme, _
context.Request.Url.Host, _
IIf(context.Request.Url.Port = 80, String.Empty, ":" & context.Request.Url.Port), _
context.Request.ApplicationPath)
End If
Return appPath
End Get
End Property
or try this (just founded):
HttpContext.Current.Request.UrlInternal

Resources