我正在尝试创建一个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;
}
}
所以现在,如果我能得到一些反馈,我会很感激。
答案 0 :(得分:1)
我不完全确定你在寻找什么。看起来你正在黑暗中寻找一些指导,所以我会从那里开始。
MVC.NET有一些非常有用的约定,您可以通过查看Microsoft的this等一些教程快速学习它们。关于Best Practices也有很多意见。
基本出发点是定义您的控制器,视图和模型(每个控制器,视图和模型都按照惯例位于同名文件夹中)。默认的MVC模板将具有HomeController和关联的视图。 Scott Gu有几篇关于MVC2的好帖子仍然有效,比如routing上的这个帖子。
但是,您决定设置MVC,请记住您的业务逻辑位于不同的层,因此您的控制器将在其他位置调用业务代码,这将调用另一层中的数据库访问代码。