why ActionResult method asking for default control parameters
So I have follow problem
I have a view ActionResults methods: Both located in one controller - TestController
public override ActionResult Index(int pageNumber, int pageSize, string nothing)
{
...
}
public ActionResult getAJAX()
{
...
}
my global.asax file:
routes.MapRoute(
"getAJAX",
"{controller}/getAJAX",
new { action = "getAJAX" }
);
routes.MapRoute(
"Test",
"{controller}/{action}/{id}",
new { controller = "Test", action = "Index", id = UrlParameter.Optional, pageNumber = 1, pageSize = 50 }
);
If I open site.com/TestController/getAJAX
- I get the following error
The parameters dictionary contains a null entry for parameter 'pageNumber' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32, Int32, System.String)'
If open site.com/TestController/getAJAX/1
- everything is OK
Why is getAJAX
asking for parameters which are in other method?
If your controller is called TestController
, then everywhere in your URLs you need to use Test
(ie Controller
should be removed from the URL), for example, use site.com/Test/getAJAX
instead of site.com/TestController/getAJAX
.
When you try to navigate to site.com/TestController/getAJAX
MVC is looking for a class called TestControllerController
, and since it is not there then second route is used instead of the first one.
Additionally, you don't need Controller
in your default route object, so instead of
new { controller = "TestController", action = "Index", id = UrlParameter.Optional, pageNumber = 1, pageSize = 50 }
you should be using
new { controller = "Test", action = "Index", id = UrlParameter.Optional, pageNumber = 1, pageSize = 50 }
As you see line System.Web.Mvc.ActionResult Index(Int32, Int32, System.String)
in error message.
It is pointing to Index(...)
action instead of getAJAX()
action.
As per MapRoute in global.asax
file, getAJAX
will work for "site.com/Test/getAjax"
And "site.com/getAJAX"
has matched with "TestController"
route as default route.
Solution: Change MapRoute for getAJAX
as
routes.MapRoute(
"getAJAX",
"getAJAX",
new { controller="Test", action = "getAJAX" }
);
链接地址: http://www.djcxy.com/p/39212.html