Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 参数字典包含参数的空条目,但参数存在_C#_Asp.net Mvc_Model View Controller_Dictionary - Fatal编程技术网

C# 参数字典包含参数的空条目,但参数存在

C# 参数字典包含参数的空条目,但参数存在,c#,asp.net-mvc,model-view-controller,dictionary,C#,Asp.net Mvc,Model View Controller,Dictionary,我的控制器中有一个简单的方法——GetMeterProfileData,带有1个参数——meterID public class AlertsDashboardController : Controller { public ActionResult GetMeterProfileData(int meterID) { List<IMeterProfileData> result = new List<IMeterProfileData>(

我的控制器中有一个简单的方法——GetMeterProfileData,带有1个参数——meterID

public class AlertsDashboardController : Controller
{
    public ActionResult GetMeterProfileData(int meterID)
    {
        List<IMeterProfileData> result = new List<IMeterProfileData>();

        {
            if (meterID == 1)
            {
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("01/01/2000 00:00"), Consumption = 102});
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("31/01/2000 00:30"), Consumption = 12});
            }
            else
            {
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("01/01/2004 00:00"), Consumption = 500});
                result.Add(new MeterProfileData() {PeriodDateTime = Convert.ToDateTime("31/01/2004 00:30"), Consumption = 445});
            }
        }

        return Json(result, JsonRequestBehavior.AllowGet);

    }
如您所见,正在指定MeterID

那么为什么我会得到这个错误呢

参数字典包含参数的空条目 方法的“System.Int32”类型不可为空的“meterID” 中的“System.Web.Mvc.ActionResult GetMeterProfileData(Int32)” 'STC.MVC.Web.Controllers.AlertsDashboardController'。可选的 参数必须是引用类型、可为null的类型或声明为 可选参数

我的路由设置如下

我必须为此方法添加新路由吗?这样做没有多大意义,因为我必须设置大量潜在的路由

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

        routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional}
            );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
            );           
    }
我还尝试添加一条新路线,如下所示,但这也不起作用

 routes.MapRoute(
            name: "Default2",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "AlertsDashboard", action = "Index", id = UrlParameter.Optional }
            );
我错过了什么


Paul

参数名称很重要,在使用默认路由时必须是
id
。您需要将方法签名更新为

public ActionResult GetMeterProfileData(int id)
或者使用正确的参数名称定义不同的路由:

routes.MapRoute(
        name: "Default2",
        url: "{controller}/{action}/{meterID}",
        defaults: new { controller = "AlertsDashboard", action = "Index", meterID = UrlParameter.Optional }
        );

您可以尝试使用-
public ActionResult GetMeterProfileData(int-id)
routes.MapRoute(
        name: "Default2",
        url: "{controller}/{action}/{meterID}",
        defaults: new { controller = "AlertsDashboard", action = "Index", meterID = UrlParameter.Optional }
        );