adsense

Friday, February 17, 2017

ASP.Net MVC generate Url of any file

If you want to get generate a URL for a given file (image)  in ASP.net MVC you can use following 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;
} 
 
 Then you can get the absolute URL by calling the method as displayed below.

ResolveServerUrl(VirtualPathUtility.ToAbsolute("~/images/YourImage.gif"),false))
 
 
Refer to this stackflow discussion for more information

 
Cheers,
Samitha
 
 

No comments:

Post a Comment