Asp.Net MVC 中的管理区域

发布于 2024-07-22 16:33:25 字数 246 浏览 8 评论 0原文

我的问题可能很明显,但我想构建一个设计良好的网络应用程序。 对于任何管理区域,管理员应该能够列出/创建/删除/修改用户、文章、帖子等...

我想知道设计应用程序的最佳方法是什么。 我应该为其中每一项创建一个控制器(/Users/Create/id 或 /Posts/Delete/id),还是在管理控制器中创建所有操作(/Administration/CreateUser/id 或 /Administration/DeletePost/ ID) ?

My question may be obvious but I'd like to build a well-designed web application.
As for any administration area, the admin should be able to list/create/delete/modify users, articles, posts, etc...

I'd like to know what is the best way to design the application.
Should I create a controller for each one of those items (/Users/Create/id or /Posts/Delete/id), or create all of the action in my Administration Controller (/Administration/CreateUser/id or /Administration/DeletePost/id) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一个人的旅程 2024-07-29 16:33:25

您应该为每个实体编写一个单独的控制器,以保持控制器类的关注点清晰分离。 如果你只有一个控制器,那么你将只有一个 Views 目录,其中包含几十个视图,并且你的控制器将包含几十个方法,这很快就会变得难以管理。

You should write a separate controller for each entity to keep a clean separation of concerns for your controller classes. If you only have one controller, then you will only have one Views directory with dozens of views, and your controller will contain dozens of methods, and that will soon become unmanageable.

冰葑 2024-07-29 16:33:25

答案取决于控制器中有多少功能。 只需从一个控制器开始,如果它太多,请将其分成几个。
MVC 的伟大之处在于,您将内容放入控制器中不必对 URL 产生任何影响。 您可以非常轻松地将 /Users/Create 映射到 UserAdminController 类。

The answer depends on how much functionality will be in the controllers. Just start of with one controller and if it gets too much split it into a few.
The great thing about MVC is where you put things in your controllers doesn't have to have any effect on the URLs. you can very easily map /Users/Create to e.g. UserAdminController class.

吻安 2024-07-29 16:33:25

我只想构建一个新的 MVC 网站来处理管理。
只要将数据和业务逻辑分离在不同的程序集中,您就拥有更灵活的解决方案。 然后,您可以将网站发布到子域,例如 admin.yoursite.com。 这样您就不必弄乱您的路线,并且可以将它们保留在单独的视图中,恕我直言,这是最优雅的解决方案。
赞成和反对的意见很高兴听到。

我正在开发一个需要相同管理站点的项目,但还没有做到这一点,所以这个问题让我很感兴趣。

I would just build a new MVC website that handles the administration.
You have a more flexible solution as long as you've separated the data and the business logic in different assembly's. You could then publish your website to a subdomain, admin.yoursite.com for example. That way you don't have to mess with your routes and you can keep them in separate views which imho is the most elegant solution.
Pro's and Con's would be nice to hear.

I'm working on a project which will need the same administration site but haven't got that far yet so this question interests me a lot.

苏辞 2024-07-29 16:33:25

我目前正在为一个大客户使用 ASP.NET。

我采用的方法是将操作的功能放入另一个类中。

示例

我也在写一个管理部分。 将有一个管理控制器(我们的管理部分很小,如果它更大,我会更改路由以允许更多控制器,目前我们正在使用开箱即用的配置)。 如果我创建一个“EditUser”视图。 我还将创建一个“EditUserAction”。 所有 EditUser 代码都将进入该类。 我在编辑用户方法的管理控制器类中构造了 EditUserAction 类。 这将从 Controller 类中删除所有特定于操作的代码。 这样,所有特定于操作的代码都位于操作方法或操作类中。 否则,控制器很快就会被各种操作的代码淹没。 控制器类很快就会变得一团乱,难以管理。

类示例

public class Administration: Controller
{
    public ActionResult EditUser(string userId)
    {
        EditUserAction action = new EditUserAction();
    }
}

public class EditUserAction
{
    public void Save(User user)
    {   
        //save code here
    }
}

我希望这个解释是清楚的。 如果没有,请告诉我,我会澄清。

为了回答你的问题,我采用后者(/Administration/CreateUser/id/Administration/DeletePost/id)。

I'm currently using ASP.NET for a large client.

The approach I've adopted is to put the functionality of the action into another class.

Example

I am writing an administration section also. There will be one Administration controller (our admin section is small, if it was larger I would change the routing to allow more controllers, for now we are using the out of the box configuration). If I create an "EditUser" view. I will also create an "EditUserAction" class. All EditUser code will go into the class. I construct the EditUserAction class in the Administration controller class in the Edit User method. This removes all the action specific code out of the Controller class. This way all the action specific code is either in the action method or in the action class. Otherwise, the controller would quickly become overrun with code from various actions. The controller class would balloon to an unmanageable mess in short order.

Class examples

public class Administration: Controller
{
    public ActionResult EditUser(string userId)
    {
        EditUserAction action = new EditUserAction();
    }
}

public class EditUserAction
{
    public void Save(User user)
    {   
        //save code here
    }
}

I hope this explanation is clear. If it's not let me know and I'll clarify.

To answer your question I am doing it the latter(/Administration/CreateUser/id or /Administration/DeletePost/id).

合约呢 2024-07-29 16:33:25

您可以使用 DynamicData 来实现此目的。 它不是 MVC,但可以与 MVC 一起使用,并且非常易于设置和使用。

You could use DynamicData for this. It isn't MVC but it can be used together with it and it is really easy to setup and use.

似狗非友 2024-07-29 16:33:25

这是问我的问题的另一种方式。

我的母版页的一部分:

<% if (!String.Equals(ViewContext.RequestContext.RouteData.GetRequiredString("controller"), "Administration")) { %>
<div>
    <!-- Some Code -->
</div> <% } %>

如您所见,在我的母版页中,我想显示页面的某些部分,具体取决于是否在管理区域工作的用户。
它仅适用于管理控制器(/Administration/CreateUser/id)...但当我使用不同的控制器作为用户或文章(/User/DeleteUser/id 或 /Article/Details/id)时,它会变得一团糟。

我更喜欢每个实体使用一个控制器,但我找不到一种方法将此方法与多个控制器结合起来。

Here is another way of asking my question.

A part of my Master Page:

<% if (!String.Equals(ViewContext.RequestContext.RouteData.GetRequiredString("controller"), "Administration")) { %>
<div>
    <!-- Some Code -->
</div> <% } %>

As you can see, in my master page, I'd like to display some part of the page, depending on the user working on the administration area or not.
It works pretty well with only the Administration Controller (/Administration/CreateUser/id)... but it becomes a big mess when I use different controller as User or Article (/User/DeleteUser/id or /Article/Details/id).

I'd prefer to use one controller per entity, but I can't find a way to bond this approach with multiple controllers.

世界等同你 2024-07-29 16:33:25

我建议使用

但我将定义更改为:

    public ThemedViewEngine()
    {
        base.MasterLocationFormats = new string[] {
            "~/Views/{1}/{0}.master", 
            "~/Views/Shared/{0}.master",
            "~/Themes/{2}/Views/{1}/{0}.master", 
            "~/Themes/{2}/Views/Shared/{0}.master",
            "~/Themes/Default/Views/{1}/{0}.master", 
            "~/Themes/Default/Views/Shared/{0}.master"
        };
        base.ViewLocationFormats = new string[] { 
            "~/Views/{1}/{0}.aspx", 
            "~/Views/{1}/{0}.ascx", 
            "~/Views/Shared/{0}.aspx", 
            "~/Views/Shared/{0}.ascx",
            "~/Themes/{2}/Views/{1}/{0}.aspx", 
            "~/Themes/{2}/Views/{1}/{0}.ascx", 
            "~/Themes/{2}/Views/Shared/{0}.aspx", 
            "~/Themes/{2}/Views/Shared/{0}.ascx",
            "~/Themes/Default/Views/{1}/{0}.aspx", 
            "~/Themes/Default/Views/{1}/{0}.ascx", 
            "~/Themes/Default/Views/Shared/{0}.aspx", 
            "~/Themes/Default/Views/Shared/{0}.ascx" 
        };
        base.PartialViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx",
            "~/Themes/{2}/Views/{1}/{0}.aspx",
            "~/Themes/{2}/Views/{1}/{0}.ascx",
            "~/Themes/{2}/Views/Shared/{0}.aspx",
            "~/Themes/{2}/Views/Shared/{0}.ascx",
            "~/Themes/Default/Views/{1}/{0}.aspx",
            "~/Themes/Default/Views/{1}/{0}.ascx",
            "~/Themes/Default/Views/Shared/{0}.aspx",
            "~/Themes/Default/Views/Shared/{0}.ascx"
        };
    }

默认主题是默认的,因此它需要存在。

目录结构为:

  • 内容
  • 主题
    • 默认
      • 内容
      • 观看次数
        • 首页
        • 博客
        • 任何应该剥皮的东西
    • 其他主题
      • 内容
      • 观看次数
        • 首页
        • 博客
        • 任何应该剥皮的东西
  • 视图
    • 文章
    • 帖子
    • 用户
    • 设置
    • 其他管理事务

I suggest to use this solution.

But I changed definition to this:

    public ThemedViewEngine()
    {
        base.MasterLocationFormats = new string[] {
            "~/Views/{1}/{0}.master", 
            "~/Views/Shared/{0}.master",
            "~/Themes/{2}/Views/{1}/{0}.master", 
            "~/Themes/{2}/Views/Shared/{0}.master",
            "~/Themes/Default/Views/{1}/{0}.master", 
            "~/Themes/Default/Views/Shared/{0}.master"
        };
        base.ViewLocationFormats = new string[] { 
            "~/Views/{1}/{0}.aspx", 
            "~/Views/{1}/{0}.ascx", 
            "~/Views/Shared/{0}.aspx", 
            "~/Views/Shared/{0}.ascx",
            "~/Themes/{2}/Views/{1}/{0}.aspx", 
            "~/Themes/{2}/Views/{1}/{0}.ascx", 
            "~/Themes/{2}/Views/Shared/{0}.aspx", 
            "~/Themes/{2}/Views/Shared/{0}.ascx",
            "~/Themes/Default/Views/{1}/{0}.aspx", 
            "~/Themes/Default/Views/{1}/{0}.ascx", 
            "~/Themes/Default/Views/Shared/{0}.aspx", 
            "~/Themes/Default/Views/Shared/{0}.ascx" 
        };
        base.PartialViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx",
            "~/Themes/{2}/Views/{1}/{0}.aspx",
            "~/Themes/{2}/Views/{1}/{0}.ascx",
            "~/Themes/{2}/Views/Shared/{0}.aspx",
            "~/Themes/{2}/Views/Shared/{0}.ascx",
            "~/Themes/Default/Views/{1}/{0}.aspx",
            "~/Themes/Default/Views/{1}/{0}.ascx",
            "~/Themes/Default/Views/Shared/{0}.aspx",
            "~/Themes/Default/Views/Shared/{0}.ascx"
        };
    }

Default theme is default so it is required to exists.

Structure of directory will be:

  • Content
  • Themes
    • Default
      • Content
      • Views
        • Home
        • Blog
        • Whatever should be skinned
    • OtherTheme
      • Content
      • Views
        • Home
        • Blog
        • Whatever should be skinned
  • Views
    • Articles
    • Posts
    • Users
    • Settings
    • Other Administration stuff
王权女流氓 2024-07-29 16:33:25

这取决于您的管理区域的规模大小,
我建议您考虑执行以下操作(或者可能记录一下)

  • 考虑您想要独立管理多少个实体,
  • 考虑每个实体将有多少操作,
  • 检查您的应用程序和管理区域之间是否存在依赖关系(用户访问,对于用户友好的 URL ),

那么您可以指定哪种方法可以帮助您,拥有一个管理控制器,实体控制器中的管理操作,或者在大型功能应用程序的情况下定义一个新的管理项目。

*如果项目规模增长很快,很快就需要大规模,我会选择第三个 - 拥有一个新的 admin mvc 项目。

我希望它能帮助你做出决定。

It depends on the scale size of your admin area,
I suggest you consider to do the following (or maybe document it a bit)

  • Consider how many entities you want to manage independently,
  • Consider how many actions each one will have,
  • Check that is there any dependency between your application and admin area (user access, for user friendly URLs )

then you can specify which approach can help you, Having one admin controller, admin actions in entity controllers, or defining a new Admin project in case of large functional applications.

*If the project scale is growing fast and soon it needs large scale, I would choose third one - having a new admin mvc project.

I hope it help you decide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文