当我输入localhost/MyController/MyActionName/1时,我在可选参数why中得到一个nul
本文关键字:nul 一个 why MyActionName MyController localhost 输入 1时 参数 | 更新日期: 2023-09-27 18:19:38
解决方案
路线
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//必须像global.asax一样为id HERE)
重现我的问题的步骤:1.创建一个新的mvc3应用程序2.转到主控制器并放入Index(int?x){return view()}3.运行应用程序4.转到url中的url类型http://localthost:someport/Home/Index/15.在控制器中插入一个断点以查看x的值6.注意,即使你把url放在x上面,也不等于1!
我只是不明白为什么我在id中得到一个null…
public ActionResult MyActionName(int? id)
{
//the id is null!!!!!!!!!!!! event thought i just entered the url below!
}
我在浏览器中输入以下urlhttp://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)"的不可为null类型"System.Int32"的参数"MvrId"的null条目。可选参数必须是引用类型、可为null的类型,或声明为可选参数。参数名称:参数描述:在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误以及错误在代码中的来源的更多信息。
异常详细信息:System.ArgumentException:参数字典包含"MedicalVariance.Controllers.MedicineManagementController"中方法"System.Web.Mvc.ActionResult Index(Int32)"的不可为null类型"System.Int32"的参数"MvrId"的null项。可选参数必须是引用类型、可为null的类型,或者声明为可选参数。参数名称:参数
确保在默认路由之前定义了路由。