gpt4 book ai didi

asp.net - MVC 区域路由 - 带文件夹的 Controller 文件夹

转载 作者:行者123 更新时间:2023-12-02 00:23:00 26 4
gpt4 key购买 nike

目前我的文件夹结构如下:

Area (folder)
- Toolkit (folder)
- Controllers (folder)
- AdminController.cs
- Views (folder)
- Admin (folder)
- Privledges (folder)
- Create.cshtml
- Edit.cshtml
- Delete.cshtml

翻译过来就是

/Toolkit/{controller}/{action}/{tool}/{id}

将操作设置为类似于 Controller ,根据传递给操作的字符串 {tool} 参数和参数 {id} 提供 View ,这是一种不好的做法吗?

我正在谈论的内容的实现:

    private const string FOLDER_PRIVILEGES = "./Privileges/";

public ActionResult Privileges(string tool, string id = "")
{
dynamic viewModel = null;
ToolViews view; // enum for the views
// Parse the tool name to get the enum representation of the view requested
bool isParsed = Enum.TryParse(tool, out view);

if (!isParsed)
{
return HttpNotFound();
}

switch (view)
{
case ToolViews.Index:
viewModel = GetIndexViewModel(); // call a function that gets the VM
break;
case ToolViews.Edit:
viewModel = GetEditViewModelById(int.Parse(id)); // sloppy parse
break;
default:
viewModel = GetIndexViewModel();
break;
}
// The folder path is needed to reach the correct view, is this bad?
// Should I just create a more specific controller even though it would
// require making about 15-20 controllers?
return View(FOLDER_PRIVILEGES + tool, viewModel);
}

当我编写 View 时,我必须确保路径名称用于文件夹

@Html.ActionLink("Edit", "./Toolkit/Admin/Priveleges/Edit", "Admin", new { id = item.id })

这似乎是一个糟糕的做法,因为如果文件夹结构发生根本变化,则需要大量维护。

但是,如果我必须将操作分解为 Controller ,那么 Controller 的数量将会很多(几乎 20 个,随着时间的推移还会添加更多)。

如果我正在做的事情是一种不好的做法,那么提供这样的路线的最佳方式是什么?

/Toolkit/Admin/Privileges/Edit/1

我想避免执行以下操作:

/Toolkit/Admin/CreatePrivileges/1
/Toolkit/Admin/EditPrivileges/1
/Toolkit/Admin/DeletePrivileges/1

如果我没有任何意义,请告诉我,因为我很难用语言表达这个问题。

最佳答案

我认为您试图在 MVC 中强加一个违背其最初意图的约定。

使用 MVC,您的 Controller 是一个名词,而您的操作是一个动词。使用您的示例,您可以:

  • 工具包(名词)- 区域
    • 管理员(名词?)- 子区域? <-- 这是一个有点时髦的
      • 权限(名词)- Controller
        • 创建(动词)- 操作
        • 编辑(动词)- 操作
        • 删除(动词)- 操作

如您所见,如果您可以将 Toolkit + Admin 视为区域 + 子区域,或者将它们合并为一个区域(TookitAdmin),那么您将回到 Controller 和操作的最初目的。

根据评论,听起来您可能已经决定走这条路。但我想指出的是,您迂回得出的结论是回到了 MVC 的根源。

顺便说一句,您是否考虑过转向 MVC4?它的Web API为 RESTful API 提供更好的支持,听起来您可能正在尝试实现这一点。

关于asp.net - MVC 区域路由 - 带文件夹的 Controller 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13317032/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com