VirtualPathUtility.ToAbsolute () VS. Url.Content ()

I work in an MVC project and saw that they are both used. I was wondering what is the difference between the two? Is it wrong to use one over the other? I understand that I have to use Url.Content (), but VirtualPathUtility.ToAbsolute () also works.

+6
asp.net-mvc
source share
2 answers

Url.Content() is part of the MVC utility method. Being there to unify and centralize utility classes, I think.

VirtualPathUtility.ToAbsolute() is a .NET Framework method. Maybe MVC uses it, we should check the source ...

Hope for help

+7
source share

If you do this conversion in the controller, I would prefer VirtualParthUtility.ToAbsolute () over Url.Content ().

Profit appears when you want a unit test controller action. Instead of calling it directly, I would define the IPathUtilities interface, say, with one implementation, using the VirtualPathUtility methods for the site in real time, and the other used some kind of layout when testing.

If you call VirtualPathUtility directly, you wonโ€™t be able to check the action method (you might have thought that some kind of smart mockery of the HttpContext would work around this, but after trying this, I couldnโ€™t find a way to do it).

+1
source share

All Articles