tags:

views:

706

answers:

4

Hi , I recieve an error when building my vs2008 .net 3.5 solution Error 1 An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'

String _XSLTPath = Page.Request.Url.Scheme 
    + "://" 
    + Page.Request.Url.Authority 
    + Page.Request.ApplicationPath.TrimEnd('/') 
    + '/' 
    + "webparts/weatherandtime/weather/xslt/RSSWeatherXSL.xsl";

The Page object seems to be higlighting in green which is not what i want. Can someone explain whats going on?

Thanks,

A: 

You try to access the non-static property Page.Request without an instance. You have to call it on an instance. Something like myPage.Request.

Daniel Brückner
@Daniel This looks like code that lives in a control. A control has the this.Page property...so this code should be valid. I think his issue is the extra parenthesis at the end of the statement.
Justin Niessner
A: 

You might want to consider using a StringBuilder to make this a little more managable:

using System.Text;

StringBuilder sb = new StringBuilder();

// if this is a control or WebPart, replace Request with this.Page.Request
sb.Append(Request.Url.Scheme);
sb.Append("://");
sb.Append(Request.Url.Authority);
sb.Append(Request.ApplicationPath.TrimEnd('/');
sb.Append("/");
sb.Append("webparts/weatherandtime/weather/xslt/RSSWeatherXSL.xsl");

String _XSLTPath = sb.ToString();
Justin Niessner
A: 

Try using Page.Context instead:

String _XSLTPath2 = Context.Request.Url.Scheme
                    + "://"
                    + Context.Request.Url.Authority
                    + Context.Request.ApplicationPath.TrimEnd('/')
                    + '/'
                    + "webparts/weatherandtime/weather/xslt/RSSWeatherXSL.xsl"; 
Jay Riggs
A: 

Are you trying to use the Page property for a control from a method or property that's defined as static?

It's hard to see exactly what's going on without seeing the full context of the code, but that would explain why you're seeing the problem in one part of the code but not in another.

Jeremy McGee