Getting Absolute URL fron an ASP.NET MVC Action

2020-02-02 05:59发布

This probably is a dummy question but I cannot find a clear indication. I have a POCO class in a MVC3 web application whose only purpose is managing the backup of some files in the server. Typically it creates a backup and returns the filename to the controller, which sends an email with the URL for downloading it. This works fine, but I cannot build the absolute URL to be sent. No matter which function I use, I always get a relative URL, like /Backup/TheFile.zip, rather than e.g. http://www.somesite.com/Backup/TheFile.zip. I tried:

VirtualPathUtility.ToAbsolute("~/Backup/SomeFile.zip");
HttpRuntime.AppDomainAppVirtualPath + "/Backup/SomeFile.zip";
Url.Content("~/Backup/SomeFile.zip");

but they all return something like /Backup/SomeFile.zip. Any idea?

7条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-02-02 06:54

The built-in helpers in MVC 4 create absolute URLs if either the host or protocol parameters are non-empty. See this answer here with an example extension method for use in views.

查看更多
登录 后发表回答