简体   繁体   English

Request.Url.Segments [3]不适用于IIS 7

[英]Request.Url.Segments[3] not working with IIS 7

Request.Url.Segments[3] returns pagename. Request.Url.Segments[3]返回页面名称。 at my demo server have IIS6 and its working fine but at my live server had installed IIS7 and Request.Url.Segments[3] does not return page name it gives an error. 在我的演示服务器上具有IIS6并且可以正常工作,但是在我的活动服务器上已安装IIS7并且Request.Url.Segments[3]不返回页面名称,它给出了错误。

Please help me. 请帮我。 Thanks in advance 提前致谢

This has nothing to do with the version of IIS. 这与IIS的版本无关。 Most likely, you have a different setup for the site. 您很有可能为该站点设置了其他设置。

For example, on dev, by default on Visual Studio F5 debug, the site runs at: 例如,在开发人员上,默认情况下在Visual Studio F5调试中,该站点运行在:

http://localhost:1234/sitename/folder/page.aspx
                     |\_______/\_____/\_______/
                     0    1       2       3 

On test/production, you may not have the first folder: 在测试/生产中,您可能没有第一个文件夹:

http://sitename.corpo.com/folder/page.aspx
                         |\_____/\_______/
                         0   1      2    

There are better ways of getting the last element of an array dynamically, even without knowing its length. 有更好的方法可以动态获取数组的最后一个元素,即使不知道其长度也是如此。 Try using: 尝试使用:

Request.Url.Segments.Last()

Or, without linq, 或者,如果没有linq,

Request.Url.Segments[Request.Url.Segments.Length - 1]

If you are not looking for the last element, you may have to come up with a better way of specifying what you need. 如果您不查找最后一个元素,则可能必须提出一种更好的方法来指定所需的内容。

The value for each item in .Segments is entirely dependent on the Url, which is dictated by how the site is set up in IIS. .Segments每个项目的值完全取决于Url,而Url由IIS中网站的设置方式决定。

For instance, having your site at the root of the webserver will give different reseults to having the site under a virtual root. 例如,将您的网站放在网络服务器的根目录下,将使该网站位于虚拟根目录下会产生不同的结果。

In the url http://myserver/mypage.aspx , the page is at item index 1. In the url http://myserver/myvirtualroot/mypage.aspx , the page is at item index 2. 在url http://myserver/mypage.aspx中 ,该页面位于项目索引1。在url http://myserver/myvirtualroot/mypage.aspx中 ,该页面位于项目索引2。

You can demostrate this for yourself as follows: 您可以自己对此进行演示,如下所示:

var uri = new Uri("http://myserver/mypage.aspx");
for (var i = 0; i < uri.Segments.Length; i++)
{
    Console.WriteLine("Segments[{0}]: {1}",i,  uri.Segments[i]);
}

This gives: 这给出:

Segments[0]: /
Segments[1]: mypage.aspx

now try it again for 现在再试一次

var uri2 = new Uri("http://myserver/myvirtualroot/mypage.aspx");

this time you'll get: 这次您将获得:

Segments[0]: /
Segments[1]: myvirtualroot/
Segments[2]: mypage.aspx

The point is that you can't reliably say "item index 3 is the page" unless you know that the actual site url (and hence its configuration in IIS) is the same across environments. 关键是您不能可靠地说“项目索引3是页面”,除非您知道实际的站点URL(因此在IIS中的配置)在环境之间是相同的。 (Even then. it is not a particularly nice way of doing it as it is so brittle to changes) (即使那样。这样做也不是一种特别好的方法,因为它非常容易更改)

As Kobi points out, simply using uri.Segments.Last() will get you the last segment (without the querystring if there is one) which is exactly what you want. 正如Kobi所指出的,仅使用uri.Segments.Last()即可为您提供最后一个所需的最后一个段(如果有,则没有查询字符串)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM