tags:

views:

81

answers:

3

I've been using the following code in my Web form code-behind classes. I want to use it in my regular .cs files: for example, when making database calls. But when I try to use it outside of a Web form I get the following error when attempting to map with base.Request.ApplicationPath: 'object' does not contain a definition for 'Request'. What would be the simple, correct way to map to the root in a regular .cs class file in a Web Application Project?

    protected void LogError(string errMsg, string errTrace)
    {
        string mailSubject = "oops!";
        string mailBody = errMsg + errTrace;

        System.Configuration.Configuration config
            = WebConfigurationManager.OpenWebConfiguration(base.Request.ApplicationPath);
        AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");

        // Extract values from the web.config file
        string toAddress = "[email protected]";
        string fromAddress = appSettings.Settings["FromEmailAddr"].Value;
        string emailHost = appSettings.Settings["EmailHost"].Value;

        mailProvider.SendMail(fromAddress, toAddress, mailSubject, mailBody.ToString());
    }
+1  A: 

HttpContext.Current.Request.ApplicationPath.

John Saunders
Thanks John :-)
IrishChieftain
+1  A: 

You can use the HttpContext.Current class for referencing the current instance of a page from outside a Page class.

In your case, you want to use:

HttpContext.Current.Request

Instead of:

base.Request
Dan Herbert
That's a cool gravatar! Thanks for the solution, John just pipped you at the post :-)
IrishChieftain
+1  A: 

you are referencing "base" which referenced the Page or UserControl Object when living inside the UI code. Now that you moved it out of that code, the new class does not derive from either of those classes so you will have to use HttpContext.Current.Request instead.

CSharpAtl