使用MVC框架中要注意的问题(二):将Model和Controller单独用一个项目设计
2009-12-27 20:45
537 查看
这个问题很多朋友都会问到,MVC让分工协作成为了可能。但如果所有代码和页面都在一个项目中的话,那么分工就会受到限制。其实,Model和Controller都可以单独用一个(或者多个)程序单独来做。
1. Model
Model主要负责数据读写。现在我们一般可以直接用LINQ TO SQL类型或者Entity Framework 模型来做。
严格来说,Model也分为两个部分,一个是业务实体定义(这是需要与View共享的),一个是针对业务实体的操作。只不过,上面提到的两种做法将他们合二为一了。
Model项目单独做好之后,只要在MVC Web Application中添加引用即可。很简单,也很自然。
2. Controller
相对来说这就稍微复杂一些。请遵循下面的规范
Controller的名字要满足规范,例如BlogController。Blog是Controller的名字,而Controller是后缀。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
在global.asax文件中添加命名空间的注册,让MVC引擎知道如何查找Controller
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1. Model
Model主要负责数据读写。现在我们一般可以直接用LINQ TO SQL类型或者Entity Framework 模型来做。
严格来说,Model也分为两个部分,一个是业务实体定义(这是需要与View共享的),一个是针对业务实体的操作。只不过,上面提到的两种做法将他们合二为一了。
Model项目单独做好之后,只要在MVC Web Application中添加引用即可。很简单,也很自然。
2. Controller
相对来说这就稍微复杂一些。请遵循下面的规范
Controller的名字要满足规范,例如BlogController。Blog是Controller的名字,而Controller是后缀。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace MyControllers { public class BlogController:Controller { public ActionResult Index() { return View(); } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
在global.asax文件中添加命名空间的注册,让MVC引擎知道如何查找Controller
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication2 { // Note: For instructions on enabling IIS6 or IIS7 classic mode, // visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" }, // Parameter defaults new[] {"MyControllers"} ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- 使用MVC框架中要注意的问题(二):将Model和Controller单独用一个项目设计
- OpenCV 2 学习笔记(13): 算法的基本设计模式<4> :使用Model-View-Controller模式创建一个应用程序
- 通过项目中定位的一个问题:学习ionic框架$ionicPopover的使用以及注意事项
- 如何使用Maven的archetype快速生成一个新项目(解决生成项目目录不完整问题)
- 存储过程里使用select要注意的一个问题!
- php中使用$_REQUEST需要注意的一个问题
- 在设计页面时候要注意的一个问题。
- 使用switch...case需要注意的一个问题
- 使用MVC框架中要注意的问题(三):如何为链接指定路径
- 解决Protege使用中的一个问题:从已有owl文件建立项目失败(w3china)
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 使用MVC框架中要注意的问题(六):何时使用PartialView方法
- 使用MVC框架中要注意的问题(七):HtmlAttributes
- [JavaScript]使用块级作用域声明对象时要注意的一个问题
- 使用Excel2007去重复功能时要注意的一个问题
- springmvc+mybatis+maven项目集成的时候遇到一个问题,项目搭起来以后,http请求怎么都进不到controller中,页面直接返回404错误,控制台没有报任何错误,请大神帮我看看
- 使用Gdiplus::Graphics时要注意一个问题
- 使用jenkins进行git多分支项目打包时的一个小问题
- 使用MVC框架中要注意的问题(五):如何在页面和用户控件之间传递数据
- 多线程时使用shared_from_this的一个注意问题