Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Server.MapPath - Throws an exception in Windows Application ( Server is  not declared)

Posted on 2010-01-11
17
Medium Priority
?
673 Views
Last Modified: 2013-11-18

Server.MapPath - Throws an exception in Windows Application ( Server is  not declared)

I am working on a Migration / Conversion Project from Webservice to Class Layer ..!

In our existing webservice project - We have mentioned Server.MapPath("F I L E N AM E")

Now, i have commented  - Inherits System.Web.Services.WebService - On doing so, I am not able to fix the Server.MapPath - Syntax issue.
0
Comment
Question by:chokka
  • 9
  • 8
17 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26287561
In what context is the code that's throwing the exception, in a WebMethod body?  Can you post your code?
0
 

Author Comment

by:chokka
ID: 26287797


Please find the code.

At present, I am passing the direct file path
'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt"))
            Return New MedISDAL().GetMembersAddressAsXML(filter, "C:\\ClassProject\\MedISServices\\MedISServices\\App_Code\\MemberAddressReport.xslt")

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26287809
Can you post the complete code, including Imports statements.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chokka
ID: 26287858


Here i have posted the exact code ..!

Among these codes , I have commented following codes (As we need to migrate from Webservice to Class Layer)


'Imports System.Web.Caching
'Imports System.Web.Services
'Imports System.Web.Services.Protocols

 '<System.Web.Services.WebService(Namespace:="http://tempuri.org/MedIS.Web/MedISServices")> _
'Inherits System.Web.Services.WebService

 'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt"))
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web


'Imports System.Web.Caching
'Imports System.Web.Services
'Imports System.Web.Services.Protocols


Namespace MedIS.Web



    '<System.Web.Services.WebService(Namespace:="http://tempuri.org/MedIS.Web/MedISServices")> _
    Public Class MedISServices
        'Inherits System.Web.Services.WebService




 '<WebMethod(Description:="Get Member Address report in MS Excel Format."), SoapHeader("Credentials")> _
        Public Function GetMembersAddressAsXML(ByVal filter As String) As Byte()
            SecurityHelper.VerifyCredentials(Me)

            'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt"))
            Return New MedISDAL().GetMembersAddressAsXML(filter, "C:\\ClassProject\\MedISServices\\MedISServices\\App_Code\\MemberAddressReport.xslt")

        End Function

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26287976
I don't understand why you've commented all those lines out. "Server" is defined in System.Web.Services.WebService, and since you've commented out the "Inherits" line you've also removed everything defined in System.Web.Services.WebService, including "Server"

You can use System.Web.HttpContext.Current.Server.MapPath() instead, but it will only work if it's used inside of some sort of web application or web service.
0
 

Author Comment

by:chokka
ID: 26288034


Ok .. as i said you before that i am migrating or converting a Webservice Project to Normal Class layer Project.

We are removing off - Webservice Layer Permanently.

When i am undergoing the migration, I have to comment  those Web dll's.

Now, i am looking for alternate syntax instead of Server.MapPath("") to place it in Class layer.

I have an option - By mentioning the file path in Config file and passing the Key.

But i am looking for perfect syntax replacement for Server.MapPath("")
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26288101
Is this class going to be used in a web application or web service?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26288107
...or are you moving to a Windows Forms or Console based application?
0
 

Author Comment

by:chokka
ID: 26288138


Our existing application is Smart Client - Which is depend on Webservice layer.

Now, we are moving to Windows Forms Application ..! We are removing Smart Client feature .. by remodifying the Webservice Layer ..!
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26288195
The reason I ask is that Server.MapPath specifically converts a web page style path into a physical path on the server. For example, I can use Server.MapPath("/images/picture.gif") to get "C:\InetPub\wwwroot\myWebAppFolder\image\picture.gif".  So, you can see Server.MapPath() is useless outside of a web-based application of some sort.  There is no Server.MapPath outside of a web service or web application.

Where do you intend to keep MemberAddressReport.xslt? If it's in the same folder as the program (or the current working director), you should be able to refer to it by filename without any path information.

If you want to keep MemberAddressReport.xslt in the same folder as the program is running from, you can use Application.ExecutablePath to get, for example, "C:\Program Files\Your Company Name\Your Product Name"
0
 

Author Comment

by:chokka
ID: 26288285


 "C:\Program Files\Your Company Name\Your Product Name" - This is going to be in deployment file path ?

I think - Once we go for Class Layer Project - We will be using Dll's as Project Reference to the Main Project or Start up - Project.

So, we just need to keep "MemberAddressReport.xslt" in local working directory folder,



0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26291740
If you want the .xslt in the current working directory, then you simply remove Server.MapPath (and make sure the Member...xslt is in the current working directory): Return New MedISDAL().GetMembersAddressAsXML(filter, "MemberAddressReport.xslt")

0
 

Author Comment

by:chokka
ID: 26294449
(filter, "MemberAddressReport.xslt") - Incorrect file path ..!

Though i have placed in the working directory ..! It is expecting the file path ...!
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 26295823
1. Store the file in the same folder as the executable (not the class library).  Use Application.ExecutablePath + "\MemberAddressReport.xslt"

2. Store the file in the current working directory (which is not necessarily the same folder as where the exe is installed) and use "MemberAddressReport.xslt". To see the current working directory, try MessageBox.Show(System.IO.Directory.GetCurrentDirectory()).

3. Store the file in the same folder as the class library and use System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "\")) + "\MemberAddressReport.xslt"
0
 

Author Comment

by:chokka
ID: 26296726
i am just checking ..!   You have mentioned 3 different options.

lets make it crystal clear .. a simplest way .. what you suggest ..
i just want to put in the project working folder and generate a dll and use it as reference
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26296803
Option 3.  Make sure the file is really in the same folder as the DLL.
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web


Namespace MedIS.Web



    '<System.Web.Services.WebService(Namespace:="http://tempuri.org/MedIS.Web/MedISServices")> _ 
    Public Class MedISServices
        'Inherits System.Web.Services.WebService 




        '<WebMethod(Description:="Get Member Address report in MS Excel Format."), SoapHeader("Credentials")> _ 
        Public Function GetMembersAddressAsXML(ByVal filter As String) As Byte()
            SecurityHelper.VerifyCredentials(Me)

            'Return New MedISDAL().GetMembersAddressAsXML(filter, Server.MapPath("MemberAddressReport.xslt")) 
            Return New MedISDAL().GetMembersAddressAsXML(filter, GetCurrentDLLPath() + "\MemberAddressReport.xslt")

        End Function

        Private Shared Function GetCurrentDLLPath() As String
            Return System.IO.Path.GetDirectoryName( _
                System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/", "\"))
        End Function
    End Class
End Namespace

Open in new window

0
 

Author Closing Comment

by:chokka
ID: 31675739
Helpful,
Thanks
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question