gpt4 book ai didi

c# - 当我输入 localhost/MyController/MyActionName/1 时,我在可选参数中得到一个空值,为什么?

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:06 25 4
gpt4 key购买 nike

解决方案

路线

 routes.MapRoute(
"Whatever", // Route name
"{controller}/{action}/{id}", //{ID} MUST BE USED IN YOUR CONTROLLER AS THE PARAMETER
new { controller = "MyController", action = "MyActionName", id = UrlParameter.Optional } // Parameter defaults
);

就是这样!我想你必须在public actionresult(int id//必须是ID HERE like global.asax)

Steps to reproduce my problem: 1. Create a new mvc3 application2. Go to home controller and put Index(int? x){ return view()}3. Run the application4. Go to the url type in the url http://localthost:someport/Home/Index/15. Insert a break point in controller to see the value of x 6. Notice that even if you put the url above x NOT equal to 1 as is suppose to!

I just dont understand why i am getting a null in the id....

 public ActionResult MyActionName(int? id)
{
//the id is null!!!!!!!!!!!! event thought i just entered the url below!
}

我在浏览器中输入以下网址 http://locahost/MyController/MyActionName/1

//我也把它放在我的 global.asax 中,但它并没有真正帮助。

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

还有!我的错误,如果我把

  public ActionResult MyActionName(int id)
{
//the id is null!!!!!!!!!!!! event thought i just entered the url below!
}

请注意,上面的示例是为了简单起见,此错误是针对实际应用的。

“/”应用程序中的服务器错误。

对于“MedicalVariance.Controllers.MedicineManagementController”中的方法“System.Web.Mvc.ActionResult Index(Int32)”,参数字典包含不可为空类型“System.Int32”的参数“MvrId”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.ArgumentException:参数字典包含“MedicalVariance”中方法“System.Web.Mvc.ActionResult Index(Int32)”的不可为空类型“System.Int32”的参数“MvrId”的空条目.Controllers.MedicineManagementController'。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:参数

最佳答案

确保您的路由在默认路由之前定义。

关于c# - 当我输入 localhost/MyController/MyActionName/1 时,我在可选参数中得到一个空值,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8750507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com