Shivani
Shivani

Reputation: 216

Url.Action in controller generates port twice on Server

I referred this existing answer - but it didn't work.

I am using below piece of code to generate a fully qualified url and pass it to script and use this url there to redirect.

 @section Scripts
 {
    @Html.Script(@<script type="text/javascript">
    $(document).ready(function () {
        let targetUrl = '@Html.Raw(Url.Action("action", "controller",
            new RouteValueDictionary(new { Area = "Schemes" }),
            Request.Url.Scheme,
            Request.Url.Host))';

        let script = new ListScript(targetUrl);
        script.initialize();           
    });
    </script>)

}

http://servername:9565/Controller/ActionName //^^^^^ Extra port added and appends port number 9565 twice. What could be the possible solution to this? Why this is happening?

Upvotes: 1

Views: 241

Answers (1)

Stephen
Stephen

Reputation: 3084

Try the following in Controller.

var url = Url.Action("action", "controller", new RouteValueDictionary(new { Area = "Schemes" }), HttpContext.Request.Scheme); 

In View.

@Url.Action("Action", "Controller", new RouteValueDictionary(new { Area = "Schemes" }), Context.Request.Scheme, Context.Request.Host.ToString())

Also on a side note, do you need to wrap the Url.Action in Html.Raw, you just need the string value URL right?

Upvotes: 0

Related Questions