asp.net中的根目录不需要硬编码

时间:2011-03-15 12:29:48

标签: c# asp.net

我正在尝试向用户发送邮件并向他提供身份验证链接以激活他的帐户,但我很难编码链接。有什么方法我不需要硬编码

string url = "http://localhost:3876/User/Authenticate-User.aspx?emailID=" + emailfield;

string msgBody = "<h3>Dear User</h3>" + "<p>Thanks for your interest, Kindly click on the URL below to authenticate</p><br>" +
                              "<a href='" + url + "'>Activate Link</a><br><br>" +
                              "\r\n<p>With Regards,<br>Team</p>";

2 个答案:

答案 0 :(得分:2)

您可以使用Request.Url

获取托管页面的正确地址

我发现“GetComponents”方法最灵活,因为您可以很容易地看到Intellisense的选项。

string serverAddress = "http://" + 
HttpContext.Current.Request.Url.GetComponents(UriComponents.Host, UriFormat.SafeUnescaped);
if (!string.IsNullOrEmpty(HttpContext.Current.Request.Url.GetComponents(UriComponents.Port, UriFormat.SafeUnescaped))) {
    serverAddress = serverAddress + ":" + HttpContext.Current.Request.Url.GetComponents(UriComponents.Port, UriFormat.SafeUnescaped);
}

答案 1 :(得分:1)

string ActivationLink=Request.Url.ToString().Substring(0, Request.Url.ToString().LastIndexOf('/')) + "/Authenticate-User.aspx?emailID=" + emailfield;