Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net-mvc routing issue : parameters dictionary contains a null entry for parameter

I am trying to set up custom routing with the following mapped route

edit: my full route config

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        #region FixtureAdmin

        routes.MapRoute(
            name: "FixtureEdit",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "FixtureAdmin", action = "Edit", id = UrlParameter.Optional }
        );

        #endregion

        #region Results

        routes.MapRoute(
            name: "ResultAdd",
            url: "{controller}/{action}/{fixtureId}",
            defaults: new { controller = "Result", action = "Add", fixtureId = UrlParameter.Optional }
        );

        #endregion

And my controller code

public ActionResult Add(int fixtureId)
    {
       // return model to view etc..
    }

This is coming up with the exception, even though I have specified the parameter as optional.

The parameters dictionary contains a null entry for parameter 'fixtureId'

The strange thing is, if I change the parameter of the Add action to just 'Id' then the following URL will work Result/Add/1. I'm confused, is there some default routing that is overriding my custom one? Why would changing the parameter to just 'Id' work?

Edit

Just to test, I added another parameter to the action

public ActionResult Add(int? fixtureId, int? testId)

I then edited the route accordingly and now it works, so I reckon it is an issue with default routing.

like image 881
nik0lai Avatar asked Jun 03 '13 10:06

nik0lai


3 Answers

Use a nullable int in your Controller Action.

public ActionResult Add(int? fixtureId)
{
   // return model to view etc..
}

But the question is, if that is indeed an ID, how would it react if a null/blank ID is requested by the user? Is that ID a key in your DB? You can make it nullable if you are able to handle or provide a default page if the ID is blank/null.

EDIT:

This is because ASP.NET will assume that an unidentified parameter in your request is the id, in your case, Results/Add/1, 1 is unidentified. If you want to make that code work with using fixtureId, you should use Results/Add?fixureId=1. So ultimately, it's not because of the routing, but instead, it's because of the parameter in the Action that you have.

EDIT2:

Also, what you are experiencing there is called a routing conflict. Your routes are conflicting with the Default. You can try to apply constraints.

like image 195
aiapatag Avatar answered Nov 06 '22 03:11

aiapatag


2, from your post i think your problem is putting your custom route after default, like this:

   routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
        name: "ResultAdd",
        url: "{controller}/{action}/{fixtureId}",
        defaults: new { controller = "Home", action = "Add", fixtureId = UrlParameter.Optional }

so: 1/ exception "The parameters dictionary contains a null entry for parameter 'fixtureId'" will come if you dont give the specific route name for any action link or route form because MVC will use default route to routing. So you need to give specific route name to your custom route can be worked, like this:

@Html.ActionLink("Test custom Route", "Add", "Home", new { fixtureId = 1 }, "ResultAdd")

Cheer

like image 42
user2412747 Avatar answered Nov 06 '22 03:11

user2412747


Look at this method of adding what the developer calls a 'NullableConstraint' clicky link So if the optional parameter is supplied you can do some checking on it's value.

And also look at the answer following the accepted answer for what seems a simpler, cleaner solution.

like image 28
Paul Zahra Avatar answered Nov 06 '22 03:11

Paul Zahra