【问题标题】:Trying to understand MVC routing试图理解 MVC 路由
【发布时间】:2015-09-20 03:27:23
【问题描述】:

我们有几个控制器,我们希望将它们分组在我们网站上的子文件夹(管理员)中。我们会让主页位于根级别。但是对于这些页面,我们希望我们的站点路径是这样的:

www.domain.com/Admin/{controller}/{action}/{id}

我已经这样设置了 RouteConfig.cs 文件:

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

我已经设置了一个这样的控制器:

[RoutePrefix("Admin/SystemSecurity")]
public class SystemSecurityController : Controller
{
    private MkpContext _db = new MkpContext();

    // GET: SystemSecurity
    public ActionResult Index()
    {
        var roles = _db.Role.Select(r => r);

        return View(roles.ToList());
    }
}

在我们的解决方案中,控制器的路径是:\Controllers\Admin\SystemSecurityController.cs

视图的路径是:\Views\Admin\SystemSecurity\Index.cshtml

但我们收到“找不到资源”错误消息。

我也尝试过不使用 RoutePrefix,也使用 RoutePrefix("Admin")。

如果我把视图放在这里:\Views\SystemSecurity\Index.cshtml
并使用此路径导航:www.domain.com/SystemSecurity/Index
页面加载,所以我知道控制器和页面正在工作。

我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5


    【解决方案1】:

    我发现了 MVC 区域。 (http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/)

    通过向我的项目添加区域(右键单击项目名称,然后添加 - 区域),我可以更好地对代码进行分组。

    我发现的许多页面要么没有提及地区,要么在提及时只是顺便提及,但他们没有解释它们。希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 2013-03-12
      相关资源
      最近更新 更多