问题
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,
回答1:
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
.
回答2:
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();
回答3:
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";
回答4:
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.
回答5:
Usually this is a right approach:
String _XSLTPath = HttpContext.Current.Request.Url.Scheme;
来源:https://stackoverflow.com/questions/2051558/an-object-reference-is-required-for-the-non-static-field-method-or-property