开发者

mvc parameter dictionary contains null entry ..... routing error/problem

开发者 https://www.devze.com 2023-03-19 14:07 出处:网络
So I have this controller called Cars, and it has: namespace MySite.Controllers { public class CarsController : ApplicationController

So I have this controller called Cars, and it has:

namespace MySite.Controllers
{
    public class CarsController : ApplicationController
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult New()
        {
      开发者_JAVA百科      return View();
        }
        [httppost]
        public ActionResult New()
        {
            return View();
        }


        public ActionResult Details(int id)
        {
            return View();
        }
    }
}

Global asax

       routes.MapRouteLowerCase(
            "Cars", // Route name
            "Cars/{id}", // URL with parameters
            new { controller = "Cars", action = "Details", id = URLParameter.Optional}
        );

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

So following is the error I'm getting:

the parameters dictionary contains a null entry for parameter of non nullable type for method in

NOTE: It's getting me redirected to the "Details" View and is asking for an int even thought I wanted to go to the "New" view. I'm not really sure what's happening? I basically wanted to have my urls lowercased while removing the "action" on the url on the Promoter routing...Does that make sense? Anyhelp is appreciated! Thanks!!!


All code below applies to the first route definition.
Second route definition should stay as it is.

Your routing is invalid

Take a look at the first route definition:

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);

And when you access Cars/New this first route gets hit because all parameters are easily applied as:

  • controller = "Cars"
  • action = "Details"
  • id = "New"

If you'd like the first route definition to only cover certain IDs you should put a constraint onto it or change your routing. Constraint for numeric IDs should look like this:

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details" },
    new { id = @"\d+" }
);

When you'd access Cars/New first route wouldn't be hit because New doesn't satisfy ID constraint so route processing would continue with the next route (which would resolve it just fine - as it should).

Mind the fact, that id isn't optional anymore. In case of putting a constraint onto it it can't be. If you have a Details controller action it should most probably display some certain details. So it actually needs some ID.


If your route is setted like this

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);

that means that any URL that starts with "/Cars/..." will redirect you to Details action, that includes "/Cars/New" and it will try to map the "New" string to the id parameter that is why you get the error.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号