An object reference is required for the non-static field, method, or property

﹥>﹥吖頭↗ 提交于 2019-12-31 03:48:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!