开发者

how to find local image in asp.net project?

开发者 https://www.devze.com 2023-01-28 08:09 出处:网络
I have a solution contains a web project named \"Web\", and a dependeny class library project named \"Service\". I use the ASP.Net MVC2 to build up my solution. As you know, there\'s a Content folder

I have a solution contains a web project named "Web", and a dependeny class library project named "Service". I use the ASP.Net MVC2 to build up my solution. As you know, there's a Content folder storing images and css files under the web project. Now I need to ge开发者_JAVA技巧t the stream reference of "Content\Images\anon.png" in one class of my "Service" project.

I tried

var result = new FileStream(@"Content\Images\anon.png", FileMode.Open);

and press F5 to debug, but it cannot find the file and throws an exception.

I am using VS2010, please tell me how can I access to this image. Thanks very much.


Can you try

Server.MapPath("~/Content/Images/anon.png")


You can use System.Web.VirtualPathUtility.ToAbsolute("~/Content/Images/anon.png"); or RequestContext.HttpContext.Request.MapPath as well


Visual studio makes it to a temp directory for your web app, not your solution folder/ If you publish your app on a IIS server, the Server.map will be correct.


Because it is searching for the file in Debug\Content\Images\ . Are you sure the images is really there? Make sure that your file is in that path.

0

精彩评论

暂无评论...
验证码 换一张
取 消