In the controller, i need to generate some .bmp file and same it in Images folder under root. The Image.Save() function only accepts absolute path, and i need to map my relative path.
Normally in WebForm, we could call the Server.MapPath() in the page, while the Controller.Server is an IHttpServerUtility type, that does not has the MapPath() function. I tried to cast it to HttpServerUtility tye but failed.
Which are unlikely to be very unit testable you also have properties Httpcontext and Request (and thus thir MapPath methods) inherited from Controller (and thus mockable)
Don't do this, we now have IHttpContext and IHttpRequest. You can try RequestContext.HttpContext.Request.MapPath(string) too, I haven't tested it so I don't know how it behaves.
Still propagation of bad code and a bad idea given the new features of the framework. HttpContext.Current is evil and if you use it anywhere in your mvc app you are doing something wrong because you do not need it.
Sorry for reviving an old post, but seen as there is no answer yet... In Rob Conery's webcast I saw him use a VirtualPathUtil helper class to map his paths. But I can't find where he gets that from. I love his screencasts, but the lame thing with video is,
that it's not searchable ;) Anyone got an idea there?
fight4it
Member
3 Points
5 Posts
How to map the relative URL in MVC framework?
Jan 04, 2008 01:24 PM|LINK
In the controller, i need to generate some .bmp file and same it in Images folder under root. The Image.Save() function only accepts absolute path, and i need to map my relative path.
Normally in WebForm, we could call the Server.MapPath() in the page, while the Controller.Server is an IHttpServerUtility type, that does not has the MapPath() function. I tried to cast it to HttpServerUtility tye but failed.
Can anyone provide some answer on this issue?
abombss
Member
575 Points
164 Posts
Re: How to map the relative URL in MVC framework?
Jan 04, 2008 02:38 PM|LINK
Url = "~/Images/some.bmp"
RequestContext.HttpContext.Request.ApplicationPath + Url.SubString(2)
The above will give a url from the root including a vdir that your app may be in.
rjcox
Contributor
7064 Points
1444 Posts
Re: How to map the relative URL in MVC framework?
Jan 04, 2008 02:42 PM|LINK
There are at least four ways.
1 & 2: Use static property of HttpContext:
Which are unlikely to be very unit testable you also have properties Httpcontext and Request (and thus thir MapPath methods) inherited from Controller (and thus mockable)
abombss
Member
575 Points
164 Posts
Re: How to map the relative URL in MVC framework?
Jan 04, 2008 02:47 PM|LINK
Don't do this, we now have IHttpContext and IHttpRequest. You can try RequestContext.HttpContext.Request.MapPath(string) too, I haven't tested it so I don't know how it behaves.
rjcox
Contributor
7064 Points
1444 Posts
Re: How to map the relative URL in MVC framework?
Jan 04, 2008 02:52 PM|LINK
Which is why I wrote the note around testing.
abombss
Member
575 Points
164 Posts
Re: How to map the relative URL in MVC framework?
Jan 04, 2008 02:57 PM|LINK
Still propagation of bad code and a bad idea given the new features of the framework. HttpContext.Current is evil and if you use it anywhere in your mvc app you are doing something wrong because you do not need it.
Just trying to protect the OP.
Cepheus
Member
337 Points
75 Posts
Re: How to map the relative URL in MVC framework?
Mar 26, 2008 09:33 AM|LINK
What is now the best practice to achieve that?
Thanks, Rainer
Boris2
Member
32 Points
81 Posts
Re: How to map the relative URL in MVC framework?
May 20, 2008 08:43 AM|LINK
gyan_flip
Member
129 Points
77 Posts
Re: How to map the relative URL in MVC framework?
May 20, 2008 09:06 AM|LINK
Can we able to use the above code in Controller class?
gyan_flip
Member
129 Points
77 Posts
Re: How to map the relative URL in MVC framework?
May 20, 2008 09:09 AM|LINK
Is the above code works?
I think the better way to do it is as : HttpContext.Request.PhysicalApplicationPath + strYourImageFolderName\Image;