开发者

Whats the right way to map path to image in MVC static method

开发者 https://www.devze.com 2023-04-10 09:10 出处:网络
Struggling with this for a bit... I read around SO and found that I should be using HostingEnvironment.MapPath().

Struggling with this for a bit... I read around SO and found that I should be using HostingEnvironment.MapPath().

Every time I try and use this method, though, I get told virtual path not allowed.

I have a file located under (project)\Content\Images\MyImage.jpg

Image image = Image.GetInstance(HostingEnvironment.MapPath("/") + "/Content/Images.cslogo_highres.jpg");

Could not find a part of the path 'C:\inetpub\wwwroot\Content\Images\cslogo_highres.jpg'.

Image image = Image.GetInstance(HostingEnvironment.MapPath("Content") + "/Images/cslogo_highres.jpg");
开发者_开发问答

The relative virtual path 'Content' is not allowed here.

I tried a few other combinations, googled around a bit, but haven't been able to come up with something which works. I would really just like to use ResolveUrl(~/Content/Images/cslogo_highres.jpg) but it doesn't seem to be an option under a static method.

Sorry if this is trivial, but I got frustrated guess-and-checking paths.

EDIT: Answer, surprisingly not as succinct as one would expect:

Image image = Image.GetInstance(HostingEnvironment.MapPath(VirtualPathUtility.ToAbsolute("~/Content/Images/cslogo_highres.jpg")));


Image image = Image.GetInstance(HostingEnvironment.MapPath(VirtualPathUtility.ToAbsolute("~/Content/Images/cslogo_highres.jpg")));
0

精彩评论

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

关注公众号