关于C#获取根目录的那些事
最近做些优化,还可以,但是却被相对路径,绝对路径折腾的苦不堪言。今日的本地好着来,布到iis上相对地址怎么都不对,子程序的发布包名称(网站名称)怎么都拿不到,吐槽下,还不让我装IIS,我怎么调。没办法,晚上回家继续赶工,势必要和路径抗争到底~~~~~
我所能知道的获取路径的办法:
1、各种获取路径,及其对应的获取值; 这个的网站是这样的:就是当前IIS目录下面的地址所获取得地址

this.lb1.Text = Request.Url.ToString() + "<br /> \n" http://192.168.93.1:8080/Index.aspx
+ Request.CurrentExecutionFilePath + "<br /> \n" /Index.aspx
+ Request.FilePath + "<br /> \n" /Index.aspx
+ Request.ApplicationPath + "<br /> \n" /
+ Request.AppRelativeCurrentExecutionFilePath + "<br /> \n" ~/Index.aspx
+ Request.PhysicalApplicationPath + "<br /> \n" D:\vs2017_Tests\IIsTest\WebApplication1\WebApplication1\
+ HttpContext.Current.Request.Url.Host + "<br /> \n" 192.168.93.1
+ Request.PhysicalPath + "<br /> \n" D:\vs2017_Tests\IIsTest\WebApplication1\WebApplication1\Index.aspx
+ Request.RawUrl + "<br /> \n"; /
排名不分先后,都是恶势力~~~
HttpContext实在是怕他一不小心变成null,对其某些时候的操作闻风丧胆,故而先没写那么多。
2、子程序获取到的地址

就是那个4_Test 的子程序下获取到的地址。这个我随便找了个,是mvc的程序,玩心大发,想看看他们会出怎样的故事。。结果,嗯,我脑补了很多故事,打住打住,还要干活。
string str = Request.Url.ToString() + "<br /> \n" // http://192.168.93.1:8080/4_Test/Home/Index
+ Request.CurrentExecutionFilePath + "<br /> \n" // /4_Test / Home / Index
+ Request.FilePath + "<br /> \n" // /4_Test/Home/Index
+ Request.ApplicationPath + "<br /> \n" // /4_Testss
+ Request.AppRelativeCurrentExecutionFilePath + "<br /> \n" // ~/ Home / Index
+ Request.PhysicalApplicationPath + "<br /> \n" // E:\精通asp.netmvc5\4_Test\4_Test\
//+ HttpContext.CurrentHandler.ProcessRequest.Url.Host + "<br /> \n" //mvc5不支持
+ Request.PhysicalPath + "<br /> \n" // E:\精通asp.netmvc5\4_Test\4_Test\Home\Index
+ Request.RawUrl + "<br /> \n"; // / 4_Test / Home / Index
然后我尴尬了.....我一直获取不到的都是这个 4_Test但是,在这我完美的拿到了~~~~~~难受想哭,不知道怎么办。想了想是不是和这个是mvc项目有关,最近web application 的程序都不知道被我塞哪去了,重新建个好了。。。
Request.Url.ToString() + "<br /> \n" // http://192.168.93.1:8080/WebName/Index.aspx
+ Request.CurrentExecutionFilePath + "<br /> \n" // / WebName / Index.aspx
+ Request.FilePath + "<br /> \n" // / WebName / Index.aspx
+ Request.ApplicationPath + "<br /> \n" // / WebName
+ Request.AppRelativeCurrentExecutionFilePath + "<br /> \n" // ~/ Index.aspx
+ Request.PhysicalApplicationPath + "<br /> \n" // D:\vs2017_Tests\IIsTest\WebApplication2\WebApplication2\
+ HttpContext.Current.Request.Url.Host + "<br /> \n" //192.168.93.1
+ Request.PhysicalPath + "<br /> \n" //D:\vs2017_Tests\IIsTest\WebApplication2\WebApplication2\Index.aspx
+ Request.RawUrl + "<br /> \n"; /// WebName / Index.aspx
我的问题在于,无法正常的显示子程序下的文件,
用相对路径时,本地调试没有问题 src="~/images/1.jpg" ,甚至是我笔记本都是可以过去的,但是不知道为什么项目布上后中间总是缺一个字节导致当前的图片无法显示~思考把url拼接圈
故要自己拼接
终于弄好了,我希望能够完美的显示我的图片,虽然感觉方法有点low,但是还没想到更好的办法。~~想到再更新。明天去试试,继续思考~
string [] str = Request.Url.ToString().Split(new String[] { "Index.aspx" },StringSplitOptions.None); //
Image1.ImageUrl = str[0] + "/images/1.jpg";
或者 pageBase + Request.ApplicationPath + url
或者 string[] str = Request.FilePath.Split(new String[]{"/Page/feof"},StringSplitOptions.None);
str[0]
浙公网安备 33010602011771号