我有這樣的控制器:MVC路由問題:無效項
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
return View();
}
public ActionResult Edit(int accessLevel)
{
return View();
}
}
成立於RouteConfig.cs爲:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Test Edit",
url: "Test/Edit/{accessLevel}",
defaults: new { controller = "Test", action = "Edit", accessLevel = UrlParameter.Optional }
);
如果我去這個網址:
http://localhost:35689/Test/Edit/2
我得到這個錯誤:
The parameters dictionary contains a null entry for parameter 'accessLevel' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'MyProject.Mvc.Client.Controllers.TestController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
任何想法,爲什麼這是?我會認爲我提供了正確的數據類型/2
。
只要使用AttributeRouting並溝渠shitty破碎的路由表廢話。 – Phill
我嘗試過,但我得到了同樣的結果,你能提供一個樣本嗎? – Spikee
請問您可以在RouteConfig訂單中交換您的路線並試用它 – Dilip