在 C# 代码中,我需要为图像编写 src。有谁知道如何在c#中获取网站根路径?我的文件夹结构是 UI/Image
我发现当我使用
string rootpath=Page.Request.ApplicationPath;
如果在调试模式下运行应用程序,它就可以工作。但如果直接输入url运行,则不会显示图像。图像的属性是http://image/turnon.bmp http://image/turnon.bmp应该是http://localhost/image/turnon.bmp http://localhost/image/turnon.bmp
任何想法?
一个简单的方法是使用MapPath
与~
通配符:
string imagePath = MapPath("~/image/turnon.bmp");
As 丹·夏普斯特表示 https://stackoverflow.com/questions/7488360/how-to-get-website-root-path-in-c#comment36126935_7488390在评论中,自从Server
暴露的对象MapPath
方法在类库中不能直接使用,命令应该是
string imagePath = HttpContext.Current.Server.MapPath("~/image/turnon.bmp");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)