Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.Action with protocol is returning a link without domain

I'm using Url.Action to generate an absolute URL to send in an email. The link is generated inside a controller.

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

For some reason, in local, I have this result:

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

but I have this result, without the domain/https protocol, in staging/prod:

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

I'm using ASP .NET Core with "Microsoft.AspNetCore.Mvc": "1.1.0".

Application published to IIS.

like image 456
Gabriel Robert Avatar asked Dec 15 '16 01:12

Gabriel Robert


1 Answers

I used to have the same issue before and the code below works fine for me.

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

or manually

var ConfirmLink = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{Url.Action("ConfirmEmail", "Account")}";
like image 146
Hung Quach Avatar answered Sep 29 '22 05:09

Hung Quach