views:

41

answers:

3

I have the following controller, inside an area called "Service":

namespace Web.Areas.Service.Controllers
{
    public class IntervalController : Controller
    {
        //
        // GET: /Service/Interval/

        public JsonResult Monitor(String accountId, int datekey)
        {
            //...
        }
    }

}

The URL

http://localhost/Web/Service/Interval/Monitor?accountid=123&datekey=123

is returning an ASP.net error:

"The parameters dictionary contains a null entry for parameter 'accountId' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.JsonResult Monitor(System.Guid, Int32)'"

How can I set up routing to pass the parameters to the controller properly?

A: 

Hey,

It's saying you have a guid, do you potentially have another override, or do you have an action name attribute specified for one of the methods defined as Monitor?

Brian
A: 

Maybe you need to add in Global.asax: (the next lines are in VB but it's almost the same)

routes.MapRoute( _  
        "Monitor", _  
        "{controller}/{action}/{accountId}/{datekey}", _  
        New With {.controller = "Home", .action = "Index", .accountId = UrlParameter.Optional, .datekey = UrlParameter.Optional})

Look the parameters are optional so you can pass string, long, or any data type. I prefer do not write a MapRoute for each action, so try to standard it

routes.MapRoute( _  
        "WhatEver", _  
        "{controller}/{action}/{parameter1}/{parameter2}", _  
        New With {.controller = "Home", .action = "Index", .parameter1 = UrlParameter.Optional, .parameter2 = UrlParameter.Optional})

So you can use it to another action no matter the data type but it matters the numbers of parameters.

Sebastián
A: 

I looks like you have another action overload that is being matched instead of the one in your question since your current URL is throwing an error trying to match System.Web.Mvc.JsonResult Monitor(System.Guid, Int32), which obviously isn't the action you want.

public JsonResult Monitor(Guid accountid, int datekey)
{
   //...
}

Without actually seeing your routes it is a little hard to tell exactly what is going wrong. However, one thing that you should change in your URL is to make the accountid be accountId to match the case of the parameter in the Monitor action. That would make your new URL be the following:

http://localhost/Web/Service/Interval/Monitor?accountId=123&datekey=123
amurra