18

My current request is:

http://www.mofeng.com:4355/

I want display an action url in asp.net view layer, url is like this(full url, including http://protocol + hostname + port + controllerName + actionName):

http://www.mofeng.com:4355/controllerX/actionY

kiswa
  • 14,737
  • 1
  • 21
  • 29
paul cheung
  • 748
  • 2
  • 13
  • 32
  • try urlhelper `url.Action("ViewAction", "MyModelController", new { id = this.ID });` – Dave Alperovich Aug 31 '13 at 21:04
  • If your routing match the default route first (without the controller and action value) you should exclude the default parameters on the routing or create a new routing for this scenario before the defaults. – Bart Calixto Sep 01 '13 at 01:23

2 Answers2

42

enter image description here

Url.Action("Action", "Controller", null, Request.Url.Scheme);

How to include the following in the Form Action attribute?

1. Protocol(http:// or https://)
2. HostName
3. QueryString
4. Port

@{
    var actionURL = Url.Action("Action", "Controller", 
                               FormMethod.Post, Request.Url.Scheme)  
                    + Request.Url.PathAndQuery;
}
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, 
                                                   new { @action = actionURL }))
{
}
Imad Alazani
  • 6,688
  • 7
  • 36
  • 58
  • the Html.BeginForm() has the override method that can hold the 4 items above such as protocol, hostname etc. anyway it works fine, thank you. – paul cheung Sep 01 '13 at 04:22
4

besides the default route:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Application", action = "Index", id = UrlParameter.Optional }
        );

you may need to implement a new one :

        routes.MapRoute(
            name: "ControllerXActionYRoute",
            url: "controllerX/actionY",
            defaults: new { controller = "controllerX", action = "actionY" }
        );

and then you can use :

<div>@Url.Action("Action", "Controller", null, Request.Url.Scheme);</div>

*EDIT: *

to get the full url you must go to absolute.

<div>VirtualPathUtility.ToAbsolute(@Url.Action("Action", "Controller"));</div>
Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
  • this is really i should do. it seems that the port can not be got into url in this way?! – paul cheung Sep 01 '13 at 04:25
  • different port means you want to link to **another** application. In that case, how the app will know how to match the action and controller to an appropiate route ? There's no use of the Url.Action for using a different port than running **the same** application in http or https – Bart Calixto Sep 01 '13 at 05:38
  • nevermind, updated the answer to show you how to get the absolute url. – Bart Calixto Sep 01 '13 at 05:44
  • ok, after deployment, the site will hidden the port right? thank you Bart – paul cheung Sep 01 '13 at 12:14
  • yes paul. If site is on default web port (80) won't be shown. – Bart Calixto Sep 01 '13 at 20:33