Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有路由参数的ASP.Net MVC RouteAttribute错误_C#_Asp.net_Asp.net Mvc_Asp.net Mvc Routing - Fatal编程技术网

C# 带有路由参数的ASP.Net MVC RouteAttribute错误

C# 带有路由参数的ASP.Net MVC RouteAttribute错误,c#,asp.net,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Asp.net Mvc Routing,在尝试将路由映射添加到某个操作时,我遇到了很多不同的错误 我正在为get/Admin/Users/User/1 和POST/Admin/Users/User 但遗憾的是,Controller中已经有一个用户属性! 因此,我不能使用public ActionResult User(long id),因为我需要隐藏用户属性(我需要保留该属性,因为它是控制器的IPrincipal,我仍然会得到相同的错误) 以这种方式定义路由: // First in RouteConfig routes.MapMvc

在尝试将路由映射添加到某个操作时,我遇到了很多不同的错误

我正在为
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
我不确定是否真的明白出了什么问题

我查看了这页解释属性的内容,没有发现任何错误

编辑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.UsersController 类型上的System.Web.Mvc.ActionResult用户(Int64) EspaceBiere.Web.Areas.Admin.Controllers.UsersController System.Web.Mvc.ActionResult 上的用户(espacebier.Web.Areas.Admin.ViewModels.Users.UserViewModel) 键入EspaceBiere.Web.Areas.Admin.Controllers.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);
}
这是在尝试转到用户操作时发生的

在控制器上找不到公共操作方法“User” 'EspaceBiere.Web.Areas.Admin.Controllers.UsersController'

我与索引操作的链接是:

尝试使用
/Admin/Users
执行我的
索引
操作

使用
/Admin/Users/User/1
尝试
用户
操作

编辑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>
如果我在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属性之后 但是,在基于默认约定之前,已配置路由 路线已经确定。原因是路线登记应该是有序的 从最具体的(属性)到更一般的(区域 注册)到mist通用(默认路线)以避免通用 通过匹配传入路由来“隐藏”更具体的路由 请求在管道中太早

路线名称

您可以指定路由的名称,以便轻松地允许URI 这一代人。例如,对于以下路线:

您可以使用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);
    }

属性中的停止设置
Name
。仅用于生成URL。请看我在示例中的做法。您可以清楚地看到哪里出了问题。添加了另一个编辑,我们接近答案,但用户路线仍不符合我的要求@Vince,正如我之前所说,
Route.Name
用于生成URL,就像您希望使用它一样,但您使用
Name
尝试设置路由模板。不知道您也在尝试生成URL。检查更新的答案。请澄清您的答案
<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);
    }