什么是差异:

SPSite.Url 

SPSite.RootWeb.Url
.

如果两者都是相同的,这被认为是良好的做法?

有帮助吗?

解决方案

I don't believe there is a difference in terms of what they return back (the string representation of the root URL of the site).

However, in the second case you are referencing both an SPSite and SPWeb object whereas in the first you are only referencing the SPSite object (so there might be some minor performance considerations if all you are looking for is the URL to the site).

In addition I would think the the context of the code you are writing would favor one over the other (e.g. if you have code that is scoped at the site level it would make more sense to just get the SPSite object Url; if your code was scoped at the web level (for example, doing something to the root SPWeb object and then traversing down to subsequent children webs) then referencing the rootweb.url would make more consistent sense to me).

In both cases you would need to make sure you are following proper disposal practices: http://msdn.microsoft.com/en-us/library/ms468609.aspx

许可以下: CC-BY-SA归因
scroll top