We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

ASP .NET 2.0 Application Page not working from Subfolder

atljarman
atljarman asked
on
Medium Priority
529 Views
Last Modified: 2012-08-13
Hi,

I've developed a nice VB supported website using a site.master that I am developing some content management system functions (e.g., uploading images to image folder, documents to document folder, editing web pages).  All of my functions work when they are stored in the main website folder (i.e., /main/). I've also modified the web.config so that the pages are only available to authenticated users. I have also set up restrictions so that the editor pages can not be edited using the editing tools.  All of this editing will happen on a staging server (as manual edits are completed now), then will be copied over by another person to the live (public) server.

Since the person moving the files live, is not the same person doing the development, I would like to keep all of the editing files in one folder (e.g., /main/cms/) that will not be copied to the live server.   When I put my files into the /cms/ folder, I run into errors. More specifically, the files will read from other directories (e.g., /main/images), but when I try to upload or delete a file, I get the custom error page I developed and am re-directed to the default page.  

When I look at the the error line in the URL from the error page re-direct, it appears that the file is trying to submit from the main folder location. For example, when trying to delete an image using my images.aspx file in cms (i.e., /main/cms/images.aspx), it appears as if the "action" does not account for the "cms."

I've added the site.master to the cms folder and referenced correctly, with the same result.  I am wondering if I need to somehow re-write the action statement or how to resolve this issue.  I tried to add a C# script in the app_folder as my latest resort, but haven't had any luck referencing it correctly in the page.  I'm not even sure that that is the correct solution and I am stuck.
Comment
Watch Question

Could you post any code you use to save the files in it's directories?

Author

Commented:
I meant to upload one of the files previously.  In this attachment, I've uploaded the two files.  The first one displays the contents of the document folder.  If the user clicks on the document name, it displays the document.  If the user clicks on delete, it will delete when the file is in the main folder but not in the cms (i.e., sub folder).  The second, is the file to upload documents to the document folder.  Again, this will work from the main folder, but not from the sub folder.
'This is the page code to view documents in the document folder.

<%@ Page Language='VB' MasterPageFile='site.master' Title='Content Page for documents.aspx' %>

<%@ Import Namespace="System.IO" %>
<script runat='server'>
  Sub Page_Load(sender as Object, e as EventArgs)
    If Not Page.IsPostBack then
                                                                           
	Dim dirInfo As New DirectoryInfo(Server.MapPath("~\documents\"))

'      Dim dirInfo as New DirectoryInfo(Server.MapPath("."))


'articleList.DataSource = dirInfo.GetFiles("*.aspx")
    
        articleList.DataSource = dirInfo.GetFiles("*.*")
	articleList.DataBind()
    End If
  End Sub
  
   Sub articleList_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
    ' First, make sure we're NOT dealing with a Header or Footer row
     If e.Item.ItemType <> ListItemType.Header And _
        e.Item.ItemType <> ListItemType.Footer Then
        'Now, reference the Button control that the Delete ButtonColumn 
        'has been rendered to
         Dim deleteButton As Button = e.Item.Cells(0).Controls(0)

         'We can now add the onclick event handler
         deleteButton.Attributes("onclick") = "javascript:return " & _
         "confirm('Are you sure you want to delete the file " & _
         DataBinder.Eval(e.Item.DataItem, "Name") & "?')"
     End If
   End Sub
  
  
  Sub articleList_DeleteFile(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
     'First, get the filename to delete
      Dim fileName As String = articleList.DataKeys(e.Item.ItemIndex)

      lblMessage.Text = "You opted to delete the file " & fileName & ".<br /><br />"

    
      File.Delete(fileName)

      'You would want to rebind the Directory's files to the DataGrid after
      'deleting the file...

       Dim dirInfo As New DirectoryInfo(Server.MapPath("~\documents\"))
         
          articleList.DataSource = dirInfo.GetFiles("*.*")
          articleList.DataBind()


     End Sub

</script>

<asp:Content ID='Content1' ContentPlaceHolderID='Main' Runat='Server'>



	
<br />
<h2>Upload Documents</h2>
	<p><a href="~\_documents.aspx" runat="server">Click here to Upload Documents</a>.</p>
	


<h2>View Delete Doucments</h2>


<p>Below is a list of files in the main directory.  If you choose to delete a file, this can not be undone.</p>

 <asp:label runat="server" id="lblMessage" Font-Italic="True" ForeColor="Red" />
  <asp:DataGrid runat="server" id="articleList" Font-Name="Verdana"
      AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee"
      HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
      HeaderStyle-Font-Size="10.5pt" HeaderStyle-Font-Bold="True"
      DataKeyField="FullName"
      OnItemDataBound="articleList_ItemDataBound"
      OnDeleteCommand="articleList_DeleteFile">
    <Columns>
      <asp:ButtonColumn Text="Delete" ButtonType="PushButton" 
              CommandName="Delete" />
      <asp:HyperLinkColumn DataNavigateUrlField="Name" DataNavigateUrlFormatString="documents\{0}"
              DataTextField="Name" HeaderText="File Name" />
      <asp:BoundColumn DataField="LastWriteTime" 
              HeaderText="Last Write Time"
              ItemStyle-HorizontalAlign="Center" 
              DataFormatString="{0:d}" />
      <asp:BoundColumn DataField="Length" HeaderText="File Size"
	  	      ItemStyle-HorizontalAlign="Right" 
	  	      DataFormatString="{0:#,### bytes}" />
    </Columns>
  </asp:DataGrid>


</asp:Content>


'This is the file to upload documents to the documents folder.  The validation isn't correct, as it send validation to the client even when the file types are correct.

<%@ Page Language='VB' MasterPageFile='usphssite.master' Title='Content Page for documents.aspx' %>

<script runat='server'>


Protected Sub Button1_Click(ByVal sender As Object, _
   ByVal e As System.EventArgs)

        'Dim filepath As String = "C:\corpslinks\Veterinarian\documents"
        'Dim filepath As String = Server.MapPath("~\documents\")
        Dim filepath As String = Server.MapPath("~\documents")
   Dim uploadedFiles As HttpFileCollection = Request.Files
   Dim i As Integer = 0

   Do Until i = uploadedFiles.Count
     Dim userPostedFile As HttpPostedFile = uploadedFiles(i)

     Try
        If (userPostedFile.ContentLength > 0) Then
           Label1.Text += "<u>File #" & (i + 1) & "</u><br>"
           Label1.Text += "File Content Type: " & _
              userPostedFile.ContentType & "<br>"
           Label1.Text += "File Size: " & _
              userPostedFile.ContentLength & "kb<br>"
           Label1.Text += "File Name: " & _
              userPostedFile.FileName & "<br>"

           userPostedFile.SaveAs(filepath & "\" & _
              System.IO.Path.GetFileName(userPostedFile.FileName))

           Label1.Text += "Location where saved: " & _
              filepath & "\" & _
              System.IO.Path.GetFileName(userPostedFile.FileName) & _
              "<p>"
        End If
     Catch ex As Exception
        Label1.Text += "Error:<br>" & ex.Message
     End Try
     i += 1
   Loop
End Sub



</script>

<asp:Content ID='Content1' ContentPlaceHolderID='Main' Runat='Server'>

<p>This page is used to upload documents to the documents folder.  These will be available to reference from your page once uploaded.</p>




<h2>Return</h2>
	<p><a href="_documentlist.aspx" runat="server">Return to the document list</a>.</p>
	


<h2>Upload Doucments</h2>



<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload1"
        Display="Dynamic" ErrorMessage="Please specify an image file." SetFocusOnError="True"
        ValidationGroup="Group1"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Upload .pdf, .doc, .ppt, and .xls files only." SetFocusOnError="True" 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF|.doc|.DOC|.xls|.XLS|.ppt|.PPT|)$" 
        Display="Dynamic" ValidationGroup="Group1"></asp:RegularExpressionValidator><br />


<asp:FileUpload ID="FileUpload2" runat="server" /><br />


<asp:RegularExpressionValidator 
id="RegularExpressionValidator2" runat="server" 
ErrorMessage="Upload .pdf, .doc, .ppt, and .xls files only." 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF|.doc|.DOC|.xls|.XLS|.ppt|.PPT)$" 
ControlToValidate="FileUpload2"
Display="Dynamic" ValidationGroup="Group1">
</asp:RegularExpressionValidator>

<br />

<asp:FileUpload ID="FileUpload3" runat="server" /><br />


<asp:RegularExpressionValidator 
id="RegularExpressionValidator3" runat="server" 
ErrorMessage="Upload .pdf, .doc, .ppt, and .xls files only." 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF|.doc|.DOC|.xls|.XLS|.ppt|.PPT)$" 
ControlToValidate="FileUpload3"
Display="Dynamic" ValidationGroup="Group1">
</asp:RegularExpressionValidator>

<br />


<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
 Text="Upload File(s)" ValidationGroup="Group1"/>&nbsp;<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
<asp:Label ID="Label2" runat="server"></asp:Label><br /><br />
<asp:Label ID="Label3" runat="server"></asp:Label><br /><br />


<br />


</asp:Content>

Open in new window

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
I found the solution as C# on another site and converted it to VB.  This is functioning for my site.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.