【问题标题】:Controllers in subfolders ASP.Net MVC 4子文件夹中的控制器 ASP.Net MVC 4
【发布时间】:2013-10-31 18:48:43
【问题描述】:

在我的 ASP.Net MVC 4 项目中,我在控制器文件夹的子文件夹中有一个控制器-

/Controllers
    /GroupA
        /AbcController.cs

在 AbcController 中,我有两种方法-

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Edit(string value)
    {
        ViewBag.Message = value;
        return View();
    }

RouteConfig.cs -

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "TestRoute",
            url: "GroupA/{controller}/{action}/{id}",
            defaults: new { controller = "AbcController", action = "Index", id = UrlParameter.Optional }               
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

但是当我浏览http://localhost:2240/groupa/abc/edit/somevalue

, 'somevalue' 没有传递给方法。它显示为空。

我在这里缺少什么?

【问题讨论】:

  • 如果我没记错的话,是不是通过.../groupa/abc/edit?somevalue 作为 url 或将 public ActionResult Edit(string value) 更改为 public ActionResult Edit(string id) 来解决它,即您在路由中将参数命名为 id,但 value在方法中
  • MVC Areas 将为您的路线(“子文件夹”)提供另一个层次结构。
  • @Jasen,是的,MVC Area 是要走的路。之前我也用过OP的方式,但是最近才知道Areas,非常好用。

标签: asp.net-mvc-4


【解决方案1】:

在您的路由中,您的参数被声明为id,而在您的操作方法中,它被声明为value。选择一个并坚持下去。

routes.MapRoute(
    name: "TestRoute",
    url: "GroupA/{controller}/{action}/{value}",
    defaults: new { controller = "AbcController", action = "Index", value = UrlParameter.Optional }               
);

public ActionResult Edit(string value)
{
    ViewBag.Message = value;
    return View();
}

编辑:当我们讨论这个主题时,我建议你看看AttributeRouting。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多