ASP.NET MVC > URLs

Get url of action method in ASP.NET MVC

How to get the url of the action method?


To get the url of the action method, we can use Url helper methods.

@Url.Action("Contact", "Home")

In the above Url.Action method, the first parameter is the name of the action method and the second parameter is the name of the controller in which this action method exists.

@Url.Action("Index", "Home", new { id = 54, com = "delete", page = "5" })

The above overload method of the Url.Action has 3rd parameter as the object parameter that is used to pass the parameter to the action method of the controller.

In this case, we are passing id = 54, com = delete and page = 5. If the action (in this case Index) is defined with these 3 parameters, then the value of these parameter can be retrieved using those parameters like in below action method.

CONTROLLER ACTION METHOD

public ActionResult Index(int id, string delete, int page)
        {
            // get the value of parameters here.
            // id = 54, com = delete and page = 5

            return View();
        }

If the action method is not defined with the parameters then these parameters becomes the querystring in the url like

        /Home/Index/54?com=delete&page=5

Notice that id has not been added as querystring as id is optional in the default route defined in the App_Start/RouteConfig.cs so it is set with the url segment but com and page is not defined in the route as parameter so it becomes querystring. Note that Url.Action method only creates the url not the complete hyperlink, to create hyperlink we need to use Html.ActionLink covered next.

To access these querystring values in the action method, we can use Request.QueryString like below

CONTROLLER ACTION METHOD

public ActionResult Index()
  {
      string com = Request.QueryString["com"];
      string page = Request.QueryString["page"];
      int id = int.Parse(this.RouteData.Values["id"].ToString());

      return View();
  }

As this method has no parameters so we are accessing the “com” and “page” querystring values using Request.QueryString and “id” value as this.RouteData.Values

CONTROLLER ACTION METHOD

        public ActionResult Index(int id)
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC
application.";
            string com = Request.QueryString["com"];
            string page = Request.QueryString["page"];
            return View();
        }

If the action method is defined with id as parameter then id value directly comes with the parameter and remaining querystring values can be retrieved using Request.QueryString.

POINT OF INTEREST

Try different overload methods of the @Url.Action method and we will see that we have ability to pass objectParameters, protocols etc.

Eg. If we want to create a url starting with https, we can give protocol parameter

@Url.Action("Contact", "Home", null, "https")

Here, as we do not want to pass objectParameter so we have passed null as 3rd parameter and the 4th parameter is the “https” so our url would look like https://localhost/Home/Contact

To get complete hyperlink, use Html.ActionLink method.

 Views: 169824 | Post Order: 73



Write for us






Hosting Recommendations