![](/img/trans.png)
[英]ASP.NET MVC 5: Define route order across controllers (RouteAttribute)
[英]ASP.Net MVC RouteAttribute Error with Route Parameter
在尝试将Route映射添加到某个操作时,我遇到了很多不同的错误!
我正在尝试为GET /Admin/Users/User/1
和POST /Admin/Users/User
获取此路由
但遗憾的是,Controller中已存在用户属性! 所以我不能使用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:控制器
[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);
}
}
现在,我正在尝试进行索引操作:
以下操作方法之间的当前请求不明确:类型EspaceBiere.Web.Areas.Admin.Controllers.UsersController上的System.Web.Mvc.ActionResult Index()类型EspaceBiere.Web上的System.Web.Mvc.ActionResult用户(Int64) .Areas.Admin.Controllers.UsersController System.Web.Mvc.ActionResult类型EspaceBiere.Web.Areas.Admin.Controllers.UsersController上的用户(EspaceBiere.Web.Areas.Admin.ViewModels.Users.UserViewModel)
这是在试图去用户行动时
在控制器'EspaceBiere.Web.Areas.Admin.Controllers.UsersController'上找不到公共操作方法'User'。
我对索引操作的链接是:
尝试使用/Admin/Users
进行Index
操作
尝试使用/Admin/Users/User/1
进行User
操作
好的,我的索引现在运行正常,但我的用户操作仍然无效! 我删除了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.Action?
如果这是您的意图,您还没有完全理解使用属性路由的概念。 以下是如何配置所需路线的示例。
[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>
尼斯,
[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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.