带协议的Url.Action返回没有域的链接

时间:2016-12-15 01:52:27

标签: c# asp.net-core asp.net-core-mvc sendgrid

我正在使用Url.Action生成绝对URL以通过电子邮件发送。链接在控制器内生成。

ConfirmLink = Url.Action(
    "ConfirmEmail",
    "Account",
    new { userId = user.Id, code },
    HttpContext.Request.Scheme
)

出于某种原因,在本地,我有这个结果:

http://localhost:5102/account/confirm-email?userId=10&code=CfDJ8JH5BJKUsPZDnCLwFc3HSEHfe9Fk00NHiJqk36VbvkDbbV39AI7HY5m9pXVItwYE9%2B9ElTcnBSZiDNQ%2FmO5lx1tSnuvDmhzdzGlCQCfPQVe%2B21Nw03uNuBWoO8HCVivqzfudti579bWiaLW6PuuQQimn449YymVjgV3R36HeQID26jTmAxizDTqOsF1%2FvQc5Zw%3D%3D

但是我在staging / prod:

中没有域名/ https协议
http:///account/confirm-email?userId=10&code=CfDJ8JH5BJKUsPZDnCLwFc3HSEHfe9Fk00NHiJqk36VbvkDbbV39AI7HY5m9pXVItwYE9%2B9ElTcnBSZiDNQ%2FmO5lx1tSnuvDmhzdzGlCQCfPQVe%2B21Nw03uNuBWoO8HCVivqzfudti579bWiaLW6PuuQQimn449YymVjgV3R36HeQID26jTmAxizDTqOsF1%2FvQc5Zw%3D%3D

我正在使用带有"Microsoft.AspNetCore.Mvc": "1.1.0"的ASP .NET Core。

应用程序已发布到IIS。

2 个答案:

答案 0 :(得分:0)

尝试添加主机参数:

ConfirmLink = Url.Action(
    "ConfirmEmail",
    "Account",
    new { userId = user.Id, code },
    HttpContext.Request.Scheme,
    HttpContext.Request.Host.Host
)

答案 1 :(得分:0)

我以前遇到过同样的问题,下面的代码对我来说很合适。

var ConfirmLink = Url.Action(
    "ConfirmEmail",
    "Account",
    new { userId = user.Id, code },
    HttpContext.Request.Scheme,
    HttpContext.Request.Host.Value //HttpContext.Request.Host.ToString()
)

或手动

var ConfirmLink = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{Url.Action("ConfirmEmail", "Account")}";