asp.net-mvc - ActionResult 参数始终为 null

标签 asp.net-mvc routes

我试图在我的 Controller 中定义一个 RouteAttribute,但该值始终为 null。我试过的东西:

 [HttpGet]
 [Route("User/ChangePassword/{code:guid}")]
 public ActionResult ChangePassword(Guid code)
 {
     return View();
 }

 URL:
 http://localhost:59635/User/ChangePassword/c809619-4451-4e60-86ca-3bf7159c6d15

即使我输入了一个字符串,我得到的代码总是空的。 附言。该参数是必需的,没有可选的,有什么想法吗?

最佳答案

确保在注册默认(或区域)路由之前调用注册属性路由。应该看起来像这样......

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();

AreaRegistration.RegisterAllAreas();

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

关于asp.net-mvc - ActionResult 参数始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30333975/

相关文章:

asp.net - 在 asp.net mvc 中绑定(bind)缺少元素的数组

ruby-on-rails - 有人可以用通俗易懂的语言向我解释一下 Ruby on Rails 中映射资源和命名路由的处理方式吗?

ruby-on-rails-3 - Rails 3 根据登录状态拥有两个不同主页的最佳方式

controller - Symfony: Controller 工厂

asp.net-mvc - LINQ 父子关系

c# - 在javascript中转义内联c#脚本中的双引号

ruby - 如何将路由映射到 Sinatra 中的 Controller ?

php - KnpMenuBundle 和动态路由参数

asp.net-mvc - Asp.Net MVC 中的 Service Worker,范围

asp.net-mvc - 自定义模型绑定(bind)、模型状态和数据注释