ASP.Net MVC路由参数路由属性错误
本文关键字:路由 属性 错误 参数 ASP MVC Net | 更新日期: 2023-09-27 18:04:29
我得到了很多不同的错误,而试图添加路由映射到某个动作!
我正试图为GET /Admin/Users/User/1
获得这条路线和POST /Admin/Users/User
但遗憾的是,控制器中已经有一个User属性!所以我不能使用public ActionResult User(long id)
,因为我需要隐藏用户属性(我需要保持,因为它是控制器的IPrincipal,我仍然得到相同的错误)。
这样定义路由:
// First in RouteConfig
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// Then i'm registering my Areas.
该控制器位于Admin区域中。UsersController: Controller
[HttpGet]
[Route(Name = "User/{id:long}")]
public ActionResult GetUser(long id)
{
var model = new UserViewModel
{
User = _usersService.GetUser(id),
Roles = _rolesService.GetRoleDropdown()
};
return View("User");
}
[HttpPost]
[Route(Name = "User")]
public ActionResult GetUser(UserViewModel model)
{
if (ModelState.IsValid)
{
_usersService.UpdateUserRoles(model.User);
return RedirectToAction("Index");
}
return View("User", model);
}
这是我得到的错误:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult User(Int64)' in 'MyProjectName.Web.Areas.Admin.Controllers.UsersController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
我不确定真的明白是怎么回事!
我检查了这个解释属性的页面,我没有看到任何错误https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
编辑1
还是不行,我把注册号改成
routes.MapMvcAttributeRoutes();
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
这是我完整的控制器代码,因为现在它不工作,我的索引操作,不是在初始代码。
[RouteArea("Admin")]
[RoutePrefix("Users")]
public class UsersController : Controller
{
private readonly IUsersService _usersService;
private readonly IRolesService _rolesService;
public UsersController(
IUsersService usersService,
IRolesService rolesService)
{
_usersService = usersService;
_rolesService = rolesService;
}
[HttpGet]
[Route(Name = "Index")]
public ActionResult Index()
{
var model = new UsersViewModel
{
Users = _usersService.GetUsers()
};
return View(model);
}
[HttpGet]
[Route(Name = "User/{id:long}")]
public new ActionResult User(long id)
{
var model = new UserViewModel
{
User = _usersService.GetUser(id),
Roles = _rolesService.GetRoleDropdown()
};
return View("User");
}
[HttpPost]
[Route(Name = "User")]
public new ActionResult User(UserViewModel model)
{
if (ModelState.IsValid)
{
_usersService.UpdateUserRoles(model.User);
return RedirectToAction("Index");
}
return View("User", model);
}
}
现在,我得到,而试图去我的索引操作:
当前请求在以下操作方法之间是不明确的:System.Web.Mvc.ActionResult索引()的类型EspaceBiere.Web.Areas.Admin.Controllers.UsersControllerSystem.Web.Mvc.ActionResult用户(Int64)的类型EspaceBiere.Web.Areas.Admin.Controllers.UsersControllerSystem.Web.Mvc.ActionResult用户(EspaceBiere.Web.Areas.Admin.ViewModels.Users.UserViewModel)类型EspaceBiere.Web.Areas.Admin.Controllers.UsersController
和这个,当试图进入用户操作
在控制器上找不到公共操作方法"User"EspaceBiere.Web.Areas.Admin.Controllers.UsersController"。
到索引操作的链接是:
用/Admin/Users
尝试Index
动作
尝试/Admin/Users/User/1
for User
Action
编辑2
好的,我的索引现在工作完美,但我的用户操作仍然不工作!我已经删除了RouteAttribute的所有Name属性,以便将它们保留在构造函数中(作为模板)-> [Route("User/{id:long}")]
对不起,如果我没有看到他们在第一次阅读!
这是动作
的链接 <a href="@Url.Action("User", "Users", new { Area = "Admin", id = user.UserId })" class="btn btn-warning">
<i class="fa fa-pencil"></i>
</a>
这里是错误
No matching action was found on controller 'EspaceBiere.Web.Areas.Admin.Controllers.UsersController'. This can happen when a controller uses RouteAttribute for routing, but no action on that controller matches the request.
它确实工作,如果我写在URL/Admin/Users/User/1那么我应该如何写我的Url。行动?
您还没有完全理解使用属性路由的概念,如果这是您的意图。下面是一个如何配置所需路由的示例。
[RouteArea("Admin")]
[RoutePrefix("Users")]
public class UsersController : Controller {
private readonly IUsersService _usersService;
private readonly IRolesService _rolesService;
public UsersController(
IUsersService usersService,
IRolesService rolesService) {
_usersService = usersService;
_rolesService = rolesService;
}
//GET Admin/Users
//GET Admin/Users/Index
[HttpGet]
[Route("")]
[Route("Index")]
public ActionResult Index() {
var model = new UsersViewModel {
Users = _usersService.GetUsers()
};
return View(model);
}
//GET Admin/Users/User/1
[HttpGet]
[Route("User/{id:long}", Name = "GetUser")]
public ActionResult GetUser(long id) {
var model = new UserViewModel {
User = _usersService.GetUser(id),
Roles = _rolesService.GetRoleDropdown()
};
return View("User");
}
//POST Admin/Users/User
[HttpPost]
[Route("User")]
public ActionResult PostUser(UserViewModel model) {
if (ModelState.IsValid) {
_usersService.UpdateUserRoles(model.User);
return RedirectToAction("Index");
}
return View("User", model);
}
}
如果你同时使用带有路由属性的区域和带有基于约定的路由(由areareregistry类设置),然后你需要确保区域注册发生在MVC属性之后但是,在缺省的基于约定之前配置路由完成路由设置。原因是路由注册应该是有序的从最具体的(属性)到更一般的(区域)注册)到mist generic(默认路由)以避免generic路由从"隐藏"更具体的路由匹配传入请求在管道中太早。
// First in RouteConfig
routes.MapMvcAttributeRoutes();
// Then register Areas.
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
路线名称
您可以为路由指定一个名称,以便方便地允许URI为它而生。例如:
[Route("User/{id:long}", Name = "GetUser")]
public ActionResult GetUser(long id)
可以使用Url生成链接。RouteUrl :
<a href="@Url.RouteUrl("GetUser", new { Area = "Admin", id = user.UserId })" class="btn btn-warning">
<i class="fa fa-pencil"></i>
</a>
不错,
[HttpPost]
[Route("User")]
public ActionResult PostUser(UserViewModel model) {
if (ModelState.IsValid) {
_usersService.UpdateUserRoles(model.User);
return RedirectToAction("Index");
}enter code here
return View("User", model);
}