Get parameter by url to my controller

0

I need my controller to get a parameter from a URL ( localhost/Check/123456 ), where 12346 would be the parameter.

I configured RouteConfig:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute("index", "Check/{id}", new { controller = "Check", action = "Indice", id = UrlParameter.Optional });
    }

Controller:

public class CheckController : Controller
{
    private Check _check;

    public ActionResult Index()
    {
        //_check.NumberOfregistrationUser = Convert.ToInt16(numberOfregistrationUser);
        return View();
    }

    public ActionResult Indice(long numberOfregistrationUser)
    {
        _check.NumberOfregistrationUser = Convert.ToInt16(numberOfregistrationUser);
        return Content(Convert.ToString(_check.NumberOfregistrationUser));
    }
}
    
asked by anonymous 05.02.2018 / 18:36

1 answer

3

It is putting the action parameter with the same name that was defined in the route.

public class CheckController : Controller
{    
    public ActionResult Indice(long id) // <- trocar o nome do parâmetro
    {
        _check.NumberOfregistrationUser = Convert.ToInt16(numberOfregistrationUser);
        return Content(Convert.ToString(_check.NumberOfregistrationUser));
    }
}
    
05.02.2018 / 18:42