我想做一个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
}
似乎"一些类"one_answers"一些函数"在这里是不合适的,但我不确定在哪里把它们。任何建议吗?请告诉我粘贴整个文件是否有帮助!我想上面的内容会更简单。
编辑:谢谢你们俩的洞察力!所以我可以把它固定下来,这是类(你已经指出应该放在它们自己的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;
}
}
所以现在如果我能得到一些关于这些应该放在哪里的反馈,我将不胜感激。
我不太确定你在找什么。看起来你在黑暗中寻找指引,所以我从这里开始。
MVC。. NET有一些非常有用的约定,你可以通过看一些类似微软的教程来快速学习它们。关于最佳实践也有很多意见。
最基本的起点是定义你的控制器、视图和模型(按照约定,它们都在同名的文件夹中)。默认的MVC模板会有一个HomeController和关联的视图。Scott Gu在MVC2上有几篇很好的文章,仍然有效,比如这篇关于路由的文章。
无论您决定如何设置MVC,请记住您的业务逻辑位于不同的层中,因此您的控制器将调用其他地方的业务代码,这些代码将调用另一层中的数据库访问代码。