【问题标题】:Url.Route in ApiController doesn't include hostnameApiController 中的 Url.Route 不包含主机名
【发布时间】:2015-07-13 22:20:49
【问题描述】:

ApiController,我正在生成一个密码重置链接,该链接通过电子邮件发送给用户。除了Url.Route 函数不生成带有主机名的链接外,一切正常。我不确定是不是因为我使用的是localhost 导致了问题。

这是生成链接的代码

string callbackUrl  = Url.Route("DefaultApi", new { controller = "Account", action= "ResetPassword", UserId = user.Id, code = code });

这是生成的链接

https:///api/Account/ResetPassword?UserId=aaaaaaaaaa&code=aaaaaaaaa

注意https:///api,它应该包括主机名和端口,如https://localhost:4040/api。为什么不包含主机名?

【问题讨论】:

    标签: c# .net asp.net-mvc asp.net-web-api


    【解决方案1】:

    Url.Route 仅生成相对 URL。要获取绝对网址,请使用Url.Link:

    string callbackUrl  = Url.Link("DefaultApi", new { controller = "Account", action= "ResetPassword", UserId = user.Id, code = code });
    

    【讨论】:

      猜你喜欢
      • 2016-09-23
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      相关资源
      最近更新 更多