ASP.NET MVC 门户/CMS - 控制器和小部件设计?

发布于 2024-09-12 04:52:29 字数 1653 浏览 2 评论 0原文

我目前正在使用 jQuery portlets/sortable/draggable 开发一个门户,其中还包含内容管理系统,全部在 ASP.NET MVC 2. 只有管理员才能更改当前网站的布局/内容。

每个视图根据控制器和操作获取页面的个性化(从基本控制器)。然后视图循环遍历小部件并为每个小部件调用 renderaction。

目前,我在每个视图上都有“查看”+“编辑”操作,以将页面设置为编辑模式。当我复制代码时,一定有更好的方法,但我一生都看不到它!

您将如何实现允许编辑每个视图的操作?

public ActionResult Legal()
{
    PageModel model = GetPageSetting();
    return View("Portal", model.PageSetting.Master.Path, model);
}

[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult LegalEdit(EditorModel e)
{
    PageModel model = GetPageSetting("Legal", "Home", true);
    return View("Portal", model.PageSetting.Master.Path, model);
}

//这是在基本控制器中

protected PageModel GetPageSetting(string action, string controller, bool isEditing)
{
    PersonalizationProcess personalizationProcess = new PersonalizationProcess();

    string path = string.Format("~/{0}/{1}", controller, action);
    string userName;
    bool isAuthenticated;

    if (User == null)
    {
        userName = "TestUser";
        isAuthenticated = false;
    }
    else
    {
        userName = User.Identity.Name;
        isAuthenticated = User.Identity.IsAuthenticated;
    }

    PageSetting setting = personalizationProcess.GetPageSetting(userName, isAuthenticated, path);
    PageModel model = new PageModel();
    model.Act = action;
    model.Con = controller;
    model.IsEditing = isEditing;
    model.PageSetting = setting;
    return model;

I'm currently working on a portal using jQuery portlets/sortable/draggable that also includes a content management system, all in ASP.NET MVC 2. Only administrators are able to change the layout/content of the site currently.

Each view gets the personalization for a page (from the base controller) based on Controller and Action. Then the view loops through the widgets and calls renderaction for each of them.

Currently, I have View + "Edit" actions on each view to set the page into edit mode. As I'm duplicating code there must be a better way, but I can't see it for the life of me!

How would you implement an action that allows each View to be edited?

public ActionResult Legal()
{
    PageModel model = GetPageSetting();
    return View("Portal", model.PageSetting.Master.Path, model);
}

[HttpPost]
[Authorize(Roles = "Administrator")]
public ActionResult LegalEdit(EditorModel e)
{
    PageModel model = GetPageSetting("Legal", "Home", true);
    return View("Portal", model.PageSetting.Master.Path, model);
}

//This is in the base controller

protected PageModel GetPageSetting(string action, string controller, bool isEditing)
{
    PersonalizationProcess personalizationProcess = new PersonalizationProcess();

    string path = string.Format("~/{0}/{1}", controller, action);
    string userName;
    bool isAuthenticated;

    if (User == null)
    {
        userName = "TestUser";
        isAuthenticated = false;
    }
    else
    {
        userName = User.Identity.Name;
        isAuthenticated = User.Identity.IsAuthenticated;
    }

    PageSetting setting = personalizationProcess.GetPageSetting(userName, isAuthenticated, path);
    PageModel model = new PageModel();
    model.Act = action;
    model.Con = controller;
    model.IsEditing = isEditing;
    model.PageSetting = setting;
    return model;

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

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

发布评论

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

评论(1

简单 2024-09-19 04:52:29

在不查看代码的情况下,很难为您提供有关如何避免代码重复的具体建议。但一般来说,您想要“提取方法/提取类”,直到您无法再提取为止:)...此外,您可以使用一些 MVC 基础设施来帮助您完成一些重复的代码,即。 ModelBinder 和 ActionFilter。

也许您可以发布一些查看/编辑操作的代码来为您指明更好的方向

Without looking at the code, is hard to give you specific advice on how to avoid code duplication. But in general you want to "Extract Method/Extract Class" 'till you can't extract any more :)... Also, you can use some of the MVC infrascructure to help you do some of the repetitive code ie. ModelBinders and ActionFilters.

Maybe you can post some of the view/edit actions' code to point you in a better direction

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