Null parameter in route

1

I'm trying to get into a controller , get a parameter and print it in the View by a ViewData or ViewBag .

My Controller:

public ActionResult Index(string information)
    {
        ViewData["Bag"] = information;
        return View();
    }

My View:

@ViewData["Bag"]

The problem is that the information parameter is null every time I step in the URL call for example: http://localhost/Home/Index/teste .

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

This is the default route.

    
asked by anonymous 26.01.2016 / 16:22

2 answers

3

The problem is that your default route expects an "id" parameter and not an "information" parameter.

In action, change the parameter name to "id" or change your route so that it receives an "information" and not an "id".

    
27.01.2016 / 05:38
3

Add a new route, so you do not need to modify URI .

routes.MapRoute( name:"Information", url: "{controller}/{action}/{information}", defaults: new { controller = "Home", action = "Index", information = string.Empty });

Adding this route should solve the problem. However, it is worth remembering that another route with a string parameter may have routing problems.

    
27.01.2016 / 11:18