我在尝试将路线映射添加到特定操作时遇到了很多不同的错误!
我试图为GET /Admin/Users/User/1
获取这条路线
和POST /Admin/Users/User
但遗憾的是Controller中已有一个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.
此控制器位于管理区域中。 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/
它仍然无法正常工作,我将注册更改为
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.UsersController System.Web.Mvc.ActionResult类型的用户(Int64) EspaceBiere.Web.Areas.Admin.Controllers.UsersController System.Web.Mvc.ActionResult 用户(EspaceBiere.Web.Areas.Admin.ViewModels.Users.UserViewModel)上 键入EspaceBiere.Web.Areas.Admin.Controllers.UsersController
并尝试转到用户操作
公共行动方法'用户'在控制器上找不到 ' EspaceBiere.Web.Areas.Admin.Controllers.UsersController'
我对索引操作的链接是:
尝试使用/Admin/Users
代替Index
操作
尝试使用/Admin/Users/User/1
进行User
操作
好的,我的索引现在运行正常,但我的用户操作仍无效! 我已删除RouteAttribute的所有Name属性,以将它们保留在构造函数中(作为模板) - > 〔路线("用户/ {ID:长}&#34)]
抱歉,如果我第一次看到它时没有看到它们!
以下是操作的链接
<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.Action?
答案 0 :(得分:1)
如果这是您的意图,您还没有完全理解使用属性路由的概念。以下是如何配置所需路线的示例。
[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);
}
}
如果您同时使用具有路径属性的区域和带有的区域 基于约定的路由(由AreaRegistration类设置),然后你 需要确保在MVC属性之后进行区域注册 但是,在默认基于约定之前配置路由 路线设定。原因是应该订购路线登记 从最具体(属性)到更一般(区域 注册)以雾通用(默认路由)来避免泛型 通过匹配传入来“隐藏”更具体路线的路线 在管道中要求太早。
// 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>
答案 1 :(得分:-1)
很好,
[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);
}