缤纷多彩的植物信息世界

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

2009-12-19_201740

1.在你的MVC应用程序根目录添加新文件夹,取名为CodeSmith

2.添加CodeSmith模板文件StrongTypedViews.cst.

3.右键点击CodeSmith文件夹,选择添加一个新的CodeSmith Project,取名为CodeSmith.csp.

2009-12-19_202243

4.右键点击刚生成的CodeSmith.csp文件在弹出的右键菜单选择Manager Output

2009-12-19_202635

5.在弹出的对话框菜单做如下的设置

2009-12-19_202821

2009-12-19_203037

6.右键CodeSmith.csp选择Generate Output生成CotrollerBase.cs

stv4

ControllerBase.cs文件

namespace CNPC.Controllers
{
    public partial class ControllerBase: Controller
    {
        public class ViewFolderBase
        {
            protected readonly ControllerBase controllerBase;
            
            public ViewFolderBase(ControllerBase controllerBase)
            { this.controllerBase = controllerBase; }
        }
        
        public new ViewResult View(string path, object model)
        {
            return base.View(path, model);
        }
        
        public new PartialViewResult PartialView(string path, object model)
        {
            return base.PartialView(path, model);
        }
        
        protected ViewsFolder Views
        { get { return new ViewsFolder(this); } }
        

    public class ViewsFolder: ViewFolderBase
    {
        public ViewsFolder(ControllerBase controllerBase): base(controllerBase) { }
        

7.最好需要修改你的控制器类从ControllerBase继承,如下

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace CNPC.Controllers
{
    public class SearchController : ControllerBase//继承类
    {
        Models.cnpcDataContext cnpc = new CNPC.Models.cnpcDataContext();

        //Example Url: /Search
        //
        public ActionResult Index()
        {
            return Views.Search.Index();//强类型的读取方式

            return View();//标准的视图读取方式
        }
下载文件:https://files.cnblogs.com/apiaceae/2009_4_StrongTypedViews.zip
posted on 2009-12-19 20:48  虎克  阅读(638)  评论(0编辑  收藏  举报