1

我正在尝试制作一个 C# ASP.NET MVC4 站点。我真的不确定我的控制器文件是如何布置的。这是文件的外观:

namespace{
    *Some Classes that'll be used by the controllers
    *Controller
    *ActionResult Index(parameter)
    *IndexViewModel
    *Some functions (methods in asp.net?) that make database calls and bundle up
     the content to be sent to the view
}

似乎“某些类”和“某些功能”在这里不合适,但我不确定将它们放在哪里。有什么建议吗?让我知道它是否有助于粘贴整个文件!我认为上面会更容易。

编辑:谢谢你们的洞察力!所以我可以确定这一点,这里是类(你已经指出应该放在他们自己的cs文件中):

public class contentObject
{
    //The HTML is the content that'll be rendered on the page.
    public string theHTML { get; set; }
    //The title is the content's title.
    public string theTitle { get; set; }
}
public class utilityItem
{
    //the menu is the user-specific menu that'll be constructed
    public string menu { get; set; }
    //notes is the information about the user/company that might assist the
    //tech support people
    public string notes { get; set; }
    //notice is company-specific text that allows us to communicate with the clients.
    public string notice { get; set; }
    //companyName is the company name that'll be displayed
    public string companyName { get; set; }
}
public class listItem
{
    /* listItem is a single entry in the menu will be used to construct the entire menu, 
    which eventually gets bundled into utilityItem.*/
    public int theID { get; set; }
    public string theTitle { get; set; }
}

函数如下所示:

    public contentObject buildContent(int TopicID, int userID, HelpSiteEntities1 db)
    {
        var queryX = (from H in db.HelpTopics
                      where H.ID == TopicID
                      select new contentObject() { theHTML = H.HTML, theTitle = H.TITLE }).ToArray();
        return queryX[0];
    }
    public utilityItem buildUtility(int userID, HelpSiteEntities1 db)
    {
        var menu = "";
        var queryMenuItems = (from H in db.HelpTopics
                              join P in db.HelpTopicMaps
                              on H.ID equals P.TopicID
                              where P.UserID == userID
                              select new { theID = P.TopicID, theTitle = H.TITLE }).ToList();
        var queryVisitorInfo = (from U in db.Users
                                where U.ID == userID
                                select new { notes = U.SpecificNotes, notice = U.Notice, companyName=U.CompanyName }).ToList();
        int ittrvar = 0;
        foreach (var item in queryMenuItems)
        {
            menu += "<li><a href='/HelpTopic/Index?TopicID=" + queryMenuItems[ittrvar].theID + "'>" + queryMenuItems[ittrvar].theTitle + "</a></li>";
            ittrvar++;
        }
        var utility = new utilityItem { menu = menu, notes = queryVisitorInfo[0].notes, notice = queryVisitorInfo[0].notice, companyName=queryVisitorInfo[0].companyName };
        return utility;
    }
}

所以现在如果我能得到一些关于这些应该去哪里的反馈,我会很感激的。

4

1 回答 1

1

我不完全确定你在寻找什么。看起来你正在黑暗中寻找一些指导,所以我会从那里开始。

MVC.NET 有一些非常有用的约定,您可以通过查看一些类似 Microsoft 的教程来快速学习它们关于最佳实践也有很多意见。

最基本的出发点是定义控制器、视图和模型(按照惯例,它们都位于同名文件夹中)。默认的 MVC 模板将具有 HomeController 和关联的视图。Scott Gu 有几篇关于 MVC2 的好帖子仍然有效,比如这个关于routing的帖子。

无论您决定设置 MVC,请记住您的业务逻辑位于不同的层,因此您的控制器将在其他地方调用业务代码,这将在另一层调用数据库访问代码。

于 2013-04-22T23:50:25.547 回答