Url.Action in controller generates port twice

Guruprasad J Rao

I am using below piece of code to generate a fully qualified url and pass it back as json for redirection.

returnUrl = Url.Action("ActionName", "Controller", 
                       new RouteValueDictionary(new { type= returnUrl }), 
                       HttpContext.Request.Url.Scheme, 
                       HttpContext.Request.Url.Authority);

returnUrl will initially have a value either type1 or type2 which is why I have given type as returnUrl and then replacing its value with a generated url, but it generates

http://localhost:49518:49518/Controller/ActionName?type=type1
                     //^^^^^ Extra port added

and appends port number 49518 twice. What could be the possible solution to this? Why this is happening?

CodeNotFound

Just replace HttpContext.Request.Url.Authority with HttpContext.Request.Url.Host.

Because :

  • HttpContext.Request.Url.Authority returns the Domain Name System (DNS) host name or IP address and the port number for a server.
  • HttpContext.Request.Url.Host returns the DNS host name or IP address of the server.

In your code you are using an overload of Url.Action that accept the host name instead of the authority which contains the port.

With this fix your port will be automatically added and there will be not port duplication.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redirect to default action on unmatched controller and action in the url

From Dev

use variable for controller and action in @url.action

From Dev

URL is showing MVC controller name twice

From Dev

Ruby on Rails send_file, code in controller action running twice

From Dev

Controller action is called twice when query string ends with a dot

From Dev

laravel form generates wrong action url / route error

From Dev

RealURL: Remove Controller and Action from URL

From Dev

Pass Parameter To Controller Using URL.ACTION

From Dev

how to pass parameter with @url.action to controller

From Dev

Rails, get url to engine controller action by params

From Dev

Get controller action from url, laravel

From Dev

Url.Action is calling Controller but not return the view

From Dev

Using url parameter in the middle of action & controller

From Dev

Map legecy url to MVC Controller and action

From Dev

Remove controller and action from url yii

From Dev

Url.Action fails to call controller method

From Dev

controller getting called twice due to append params in url

From Java

useReducer Action dispatched twice

From Dev

Target action is called twice

From Dev

How to get the different URL for action methods in single controller without controller name in URL?

From Dev

How to get the different URL for action methods in single controller without controller name in URL?

From Dev

ASP.NET MVC 5 generates right url but executes wrong Action

From Java

Redirect to an external URL from controller action in Spring MVC

From Dev

@Url.Action adding "amp;" between parameters creating nulls in the controller?

From Dev

ASP.Net MVC @Url.Action() routing to the wrong controller

From Dev

How to remove controller and action name from URL in CakePHP?

From Dev

How to get @Url.Action value inside a controller

From Dev

base Url in ajax to call controller action in Yii passing parameters

From Dev

Call mvc url.action from angular controller

Related Related

  1. 1

    Redirect to default action on unmatched controller and action in the url

  2. 2

    use variable for controller and action in @url.action

  3. 3

    URL is showing MVC controller name twice

  4. 4

    Ruby on Rails send_file, code in controller action running twice

  5. 5

    Controller action is called twice when query string ends with a dot

  6. 6

    laravel form generates wrong action url / route error

  7. 7

    RealURL: Remove Controller and Action from URL

  8. 8

    Pass Parameter To Controller Using URL.ACTION

  9. 9

    how to pass parameter with @url.action to controller

  10. 10

    Rails, get url to engine controller action by params

  11. 11

    Get controller action from url, laravel

  12. 12

    Url.Action is calling Controller but not return the view

  13. 13

    Using url parameter in the middle of action & controller

  14. 14

    Map legecy url to MVC Controller and action

  15. 15

    Remove controller and action from url yii

  16. 16

    Url.Action fails to call controller method

  17. 17

    controller getting called twice due to append params in url

  18. 18

    useReducer Action dispatched twice

  19. 19

    Target action is called twice

  20. 20

    How to get the different URL for action methods in single controller without controller name in URL?

  21. 21

    How to get the different URL for action methods in single controller without controller name in URL?

  22. 22

    ASP.NET MVC 5 generates right url but executes wrong Action

  23. 23

    Redirect to an external URL from controller action in Spring MVC

  24. 24

    @Url.Action adding "amp;" between parameters creating nulls in the controller?

  25. 25

    ASP.Net MVC @Url.Action() routing to the wrong controller

  26. 26

    How to remove controller and action name from URL in CakePHP?

  27. 27

    How to get @Url.Action value inside a controller

  28. 28

    base Url in ajax to call controller action in Yii passing parameters

  29. 29

    Call mvc url.action from angular controller

HotTag

Archive