smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
585 stars 176 forks source link

@Url.Abs not working for virtual path #83

Open egbertn opened 11 years ago

egbertn commented 11 years ago

I deploy my MVC app to a IIS folder say, http://www.myhost.com/subfolder/myapp

Url.Abs now will not mapp ~/myapp to http://www.myhost.com/subfolder/myapp but it will become http://www.myhost.com/~myapp

There is an appkey in appSettings (MvcMailer.BaseURL) but I misunderstand it's value since we can do it fully automatically using code below. (Syntax @Url.AbsUrl("~/mypath/images/myimage.jpg")

public static class AbsoluteUrl { public static IHtmlString AbsUrl(this UrlHelper urlHelper, string virtualPath) { return new MvcHtmlString(ResolveServerUrl(VirtualPathUtility.ToAbsolute(virtualPath), false, urlHelper.RequestContext.HttpContext)); }

    /// <summary>
    /// mvcmailer has @Url.Abs. However, this function sucks for subfolders (eg http://www.myhost.com/mysubfolder/myapp it will not translate ~/myapp 
    /// </summary>
    /// <param name="serverUrl"></param>
    /// <param name="forceHttps"></param>
    /// <param name="context"></param>
    /// <returns></returns>
    private static string ResolveServerUrl(string serverUrl, bool forceHttps = false, HttpContextBase context = null)
    {
        if (serverUrl.IndexOf("://", StringComparison.InvariantCultureIgnoreCase) > -1)
            return serverUrl;

        string newUrl = serverUrl;
        Uri originalUri = context == null ? HttpContext.Current.Request.Url : context.Request.Url;
        newUrl = (forceHttps ? "https" : originalUri.Scheme) +
            "://" + originalUri.Authority + newUrl;
        return newUrl;
    }
}
smsohan commented 11 years ago

Please add your custom url root in the appSettings with key MvcMailer.BaseUrl

Sohan http://smsohan.com skype:smsohan | gtalk:sohan39 | cell: 403-714-2673

On Mon, Feb 4, 2013 at 5:35 AM, egbertn notifications@github.com wrote:

I deploy my MVC app to a IIS folder say, http://www.myhost.com/subfolder/myapp

Url.Abs now will not mapp ~/myapp to http://www.myhost.com/subfolder/myappbut it will become http://www.myhost.com/~myapp This might help

/// /// mvcmailer has @Url.Abs. However, this function sucks for subfolders (eg http://www.myhost.com/mysubfolder/myapp it will not translate ~/myapp /// /// /// /// /// public static string Url(string serverUrl, bool forceHttps = false, HttpContextBase context = null) { if (serverUrl.IndexOf("://", StringComparison.InvariantCultureIgnoreCase)

-1) return serverUrl;

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

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/83.