开发者

how to use Url.Content("~\stuff\hi.jpg") in controller's code?

I need the result of an Url.Co开发者_高级运维ntent("~\stuff\") in controller's code,

How do I get this?


in service code (i.e. away from the controllers), you can use:

string returnUrl = VirtualPathUtility.ToAbsolute("~/stuff/");

mvc1-3 exposes the Url.Content("~/stuff/"); from the UrlHelper in System.Web.Mvc, which can be readily used in your controller code.

[edited] - to make subtle distinction in the VirtualPathUtility.ToAbsolute("~/stuff/") usage.


Inside the controller action you could use the Url property:

public ActionResult Index()
{
    var url = Url.Content("~/stuff/");
    ...
}

Also notice the usage of / instead of \ when dealing with relative urls.


MVC 3 exposes a Url property on the controller as a UrlHelper object

var url = Url.Content("~/stuff/");

I'm not sure if it is available in older MVC versions but if not you can create your own

var urlHelper = new UrlHelper(ControllerContext.RequestContext);
var url = urlHelper.Content("~/stuff/");


To get the physical file path on disk:

Server.MapPath("~\stuff\")

Controllers also include a urlHelper, available as Url, but that may not be what you need.

What is the result that you expect?

Edit: As per your request for a FilePath, Url.Content("~\stuff\") should work? Unless you use a really old ASP.net MVC that did not have a Url property on controllers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜