ASP.NET MVC > Route

Retrieving catchall segments in action method in ASP.NET MVC

How to retrieve catchall segment value into controller action method?


Read how to define "catchall" in the route here.

To catch the route url segments value into controller action method, we use the name of the url parameter as parameter of the action method in general.

CONTROLLER CODE

public class RoutingStuffsController : Controller
    {
      public ActionResult CatchAll(string id = null, string catchall = null)
        {
            ViewBag.Action = "CatchAll";
            ViewBag.Controller = "RoutingStuffs";
            ViewBag.Id = id;
            ViewBag.CatchAll = catchall;

            return View();
        }

}

If the following url is requested from the browser

http://localhost:63087/Route/RoutingStuffs/CatchAll/50/Delete/And/Other/Parameter

it directly comes to the above action method of the RoutingStuffs controller and note that as per routing instruction in the previous point

  • Controller name - RoutintStuffs
  • Action method name – CatchAll
  • id – 50
  • catchall – “Delete/And/Other/Paramter” (notice the catchall fragments of the route defined in the previous point)

VIEW CODE

@{
    ViewBag.Title = "CatchAll";
}

<h2>Catch All</h2>

<p>Controller : @ViewBag.Controller</p>
<p>Action : @ViewBag.Action</p>
<p>Id : @ViewBag.Id</p>
<p>CatchAll : @ViewBag.CatchAll</p>

In the above view we are writing the url segments values passed to the action method.

 Views: 10417 | Post Order: 69



Write for us






Hosting Recommendations