Getting full url of any file in ASP.Net MVC

我的未来我决定 提交于 2019-11-28 19:11:45
Adeel

You can use the following code to replace "~/" to absoulute URL.

System.Web.VirtualPathUtility.ToAbsolute("~/")

Edit:

First you need to define a method.

public static string ResolveServerUrl(string serverUrl, bool forceHttps)
{
    if (serverUrl.IndexOf("://") > -1)
        return serverUrl;

    string newUrl = serverUrl;
    Uri originalUri = System.Web.HttpContext.Current.Request.Url;
    newUrl = (forceHttps ? "https" : originalUri.Scheme) +
        "://" + originalUri.Authority + newUrl;
    return newUrl;
} 

Now call this method will return the complete absolure url.

ResolveServerUrl(VirtualPathUtility.ToAbsolute("~/images/image1.gif"),false))

The output will be http://www.yourdomainname.com/images/image1.gif

new Uri(Request.Url, Url.Content("~/images/image1.gif"))

Try use this.

Url.Action("~/images/image1.gif", "/", null, Request.Url.Scheme)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!