蓝色海

导航

用EF创建一个MVC应用程序(costo university)

好了,正式开始了,刚开始就简写了,啰嗦下,为什么不直接学mVC4,个人觉得MVC4得用vs2012,而后者发布的程序好像只能运行于win2008server上,而现在不少服务器还是2003,想想还是从mvc3开始吧。

第一步:环境准备

1 vs2010 sp1

2 MVC3,vs2010需安装,自带mvc2

3 MSserver Compact 4.0(微软的嵌入式数据库,类似于Sqlite,易于部署,小项目中可以代替access,性能方面前者要差些 ,  但我的要求不是那么高,同时access和sqlite对于EF的 code first 模式支持不好,而sql Compact则可以和sqlserver一样紧密地和EF合作,sql compact还可以方便的升级到sqlserver,很方便)

4 tools for sql compact 4.0 去原文上有链接可以下载安装

 

第二步,预览下吧

第三步,ef模式的选择(对于选择我没有做过多的研究,原文的三种模式的比较就没有翻译了,觉得学一种好用就行),选择code first,虽然database first和model first没用过,但觉得code first方便简洁干净的model让我很舒服。什么是code first,待会见分晓。(也看了关于orm方面的文章,觉得很麻烦,自己还没到那个水平就下折腾了,先把基础搞好吧,等以后有时间再研究研究)

 

 

 

开始了,有些基础的久不说了,比如“如何创建一个mvc3web程序”,直接上第一段代码_layout.cshtml它是模板文件,相当于.net中的MasterPage:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
    <div class="page">
       <div id="header">
            <div id="title">
                <h1>Contoso University</h1>
            </div>

            <div id="logindisplay">
                @Html.Partial("_LogOnPartial")
            </div>
            <div id="menucontainer">
                <ul id="menu">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Students", "Index", "Student")</li>
                    <li>@Html.ActionLink("Courses", "Index", "Course")</li>
                    <li>@Html.ActionLink("Instructors", "Index", "Instructor")</li>
                    <li>@Html.ActionLink("Departments", "Index", "Department")</li>
                </ul>
            </div>
        </div>
        <div id="main">
            @RenderBody()
        </div>
        <div id="footer">
        </div>
    </div>
</body>
</html>

  这其中用到了@ViewBag ”@“是Razor的标志,Razor会自动闭合,感觉比.net<%%>好多了。ViewBag是在mvc3中新添加的,是一个dynamic类型,简单讲就是类型不定(但最后编译还是会把它变成某个类型),类似之前的ViewData但好用点速度慢了点。是Controller向View传值的一种方式。比如ViewBag.Title作为标题可以在controller中定义ViewBag.Title="例子",html中呈现的将是<title>例子</title>,当然ViewBag还有其他用处,后用到再说。

    Url.Content(WebViewPage.Url)是一个URLHelper类。Url.Content("~/Scripts/jquery-1.5.1.min.js")很简单就是讲绝对目录转换成程序的相对目录,保证在任何目录下都可以访问到相关的js资源.下面来说说这个UrlHelper类:

 它是一个帮助类,看类名也都知道这个类是用来在ASP.NET MVC应用程序中生成URL的

msdn上是这样解释:Contains methods to build URLs for ASP.NET MVC within an application.(同上)

 

The UrlHelper class provides the following methods to help you work with URLs:

  • Action. This method generates a URL that maps to an action method.通过提供的controller,action生成URL

  • RouteUrl. This method generates a URL that maps to a route.通过当前程序中的Route规则生成url

  • Content. This method generates a URL path to a resource, based on the virtual (relative) path of the resource.有一个虚拟路径生成一个资源的url路径

  • Encode. This method encodes special characters in the specified URL into character-entity equivalents.

      此方法将URl中的特殊字符转换成相等的url字符(如中文转换)

上例子:

就用此程序中例子吧

<a href='@Url.Action("Create")'>创建</a>指向action名为index的Url=》

<a href='http://127.0.0.1:2058/home/create'>创建</a>

(当然此链接完全可以用后面说到的html.actionlink代替,但二者用处不一样,后者直接生成链接,而前者则生成一个url资源定位而已)

后面实在太多,就直接拿来主义longdel的文章

<div>
     1.使用Action方法生成URL(Controller将是默认的)<br />
     <a href='@Url.Action("DemoAction")' title="">指定Action名称生成URL</a><br />
     <a href='@Url.Action("DemoAction","id")' title="">指定Action和一个RouteData(参数)生成URL</a><br />
     <a href='@Url.Action("DemoAction", new {id=2,category=5 })' title="">指定Action名称和多个参数生成URL</a><br />
     <a href='@Url.Action("DemoAction","DemoController")' title="">指定Action和Controller生成URL</a><br />
     <a href='@ Url.Action("DemoAction","DemoController","id")' title="">指定Action,Controller和一个参数生成URL</a><br />
     <a href='@ Url.Action("DemoAction","DemoController", new {id=2,category=5 })' title="">指定Action,Controller和多个参数生成URL</a><br />
     <a href='@ Url.Action("DemoAction","DemoController", new {id=2,category=5 },"https")' title="">指定传输协议生成URL</a><br />
    @{var rvd =new RouteValueDictionary();
       rvd.Add("id", 5);
       rvd.Add("category", 2);
       var tmp =5; }

<a href='@Url.Action("DemoAction","DemoController", rvd,"https","local")' title="">指定主机名生成URL</a><br /><br /> 2.使用Content方法将路径生成为程序路径<br /> <a href='@ Url.Content("~/DemoController/DemoAction")' title="">指定虚拟路径生成绝对路径</a><br /><br /> 3.使用Encode生成URL<br /> <a href='@ Url.Encode("http://www.cnblogs.com/longgel/")' title="">加密过的URL连接</a><br /><br /> 4.使用RouteUrl生成URL<br /> <a href='@ Url.RouteUrl(tmp)' title="">指定RouteValue生成URL</a><br /> <a href='@ Url.RouteUrl("Default")' title="">指定RouteName生成URL</a><br /> <a href='@ Url.RouteUrl(rvd)' title="">指定多个参数生成URL</a><br /> <a href='@ Url.RouteUrl("Default",tmp)' title="">指定路由规则名和单个路由值</a><br /> <a href='@ Url.RouteUrl("Default",rvd) ' title="">指定路由规则名和多个路由值</a><br /> <a href='@ Url.RouteUrl("Default",tmp,"https")' title="">指定传输协议</a><br /> <a href='@ Url.RouteUrl("Default",rvd,"https","www.cnblogs.com")' title="">指定主机名</a><br />

  生成了

<div>
.使用Action方法生成URL(Controller将是默认的)<br />

    <a href='/simple/DemoAction' title="">指定Action名称生成URL</a><br />

    <a href='/id/DemoAction' title="">指定Action和一个RouteData(参数)生成URL</a><br />

    <a href='/simple/DemoAction?id=2&category=5' title="">指定Action名称和多个参数生成URL</a><br />

    <a href='/DemoController/DemoAction' title="">指定Action和Controller生成URL</a><br />

    <a href='/DemoController/DemoAction?Length=2' title="">指定Action,Controller和一个参数生成URL</a><br />

    <a href='/DemoController/DemoAction?id=2&category=5' title="">指定Action,Controller和多个参数生成URL</a><br />

    <a href='https://localhost/DemoController/DemoAction?id=2&category=5' title="">指定传输协议生成URL</a><br />

    

    <a href='https://local/DemoController/DemoAction?id=5&category=2' title="">指定主机名生成URL</a><br /><br />
.使用Content方法将虚拟(相对)路径生成为绝对路径<br />

    <a href='/DemoController/DemoAction' title="">指定虚拟路径生成绝对路径</a><br /><br />
.使用Encode加密URL<br />

    <a href='http%3a%2f%2fwww.cnblogs.com%2flonggel%2f' title="">加密过的URL连接</a><br /><br />
.使用RouteUrl生成URL<br />

    <a href='/simple/urlhelperdemo' title="">指定RouteValue生成URL</a><br />

    <a href='/Longgel/Index/Id' title="">指定RouteName生成URL</a><br />

    <a href='/simple/urlhelperdemo?id=5&category=2' title="">指定多个参数生成URL</a><br />/Longgel/Index/Id<br />

    <a href='/Longgel/Index/Id' title="">指定路由规则名和单个路由值</a><br />

    <a href='/Longgel/Index/Id?id=5&category=2' title="">指定路由规则名和多个路由值</a><br />

    <a href='https://localhost/Longgel/Index/Id' title="">指定传输协议</a><br />

    <a href='https://www.cnblogs.com/Longgel/Index/Id?id=5&category=2' title="">指定主机名</a><br />        

    </div>

  上面的灰色背景中的new RoutevalueDictionary()以及RouteUrl()方法留待后解。

  还多啰嗦下关于Razor的上面的灰色背景的@{}是一个代码段

  例如

@{
       string htmlss = "<font color='red'>文本</font>";
       @Html.Raw(htmlss)
       @Url.Encode("www.baidu.com")

   <font>随便写</font> 
   }

@和{一定在一行,{}内的编码和c#一样,遇到要输出的地方加上@就可以了,里面还可以随便加html代码,自动识别的,是不是很赞啊。细节遇到再研究

今晚先写到这里,感谢longdel上上面的例子。

 

posted on 2013-01-17 20:13  蓝色海  阅读(306)  评论(0)    收藏  举报