不使用Visual Studio开发ASP.NET MVC应用(下篇)

书接上回!

前文“纯手工”、彻底抛弃Visual Studio,制作了一个ASP.NET MVC应用,运行起来还不错,项目目录、源代码、web.config等所有东西都已经做到“最简”,除去了Visual Studio生成的一大堆无关东西,当然这只是一个“起点”,随着后面项目内容和功能的扩展还需要一步步添加很多东西,但如此干净一个项目,看着就让人舒服,一砖一瓦的盖自己的房子,何尝不是一种享受!(其实很多人不认同这样,在stackoverflow原文中,问题解答者用"severe brain damage "(脑子坏了)来形容这种做法)

前文只是用C#源码编译工具csc.exe编译出了一个ASP.NET MVC应用,本篇将在增加一些非常基本的功能,Entity Framework和Master Page,这两个都是开发常规Web应用,最最基础的东西,一个用来访问数据库,一个用来构建页面框架,有了页面框架,可以使每个Controller所展现的View只关注自己要实现的内容。

一、增加Master Page

第一步,在目录/View下建立文件_ViewStart.cshtml,这个文件会在用户每次访问View时先调用,代码如下:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

第二步,建立文件夹Shared,并在里面创建文件_Layout.cshtml,内容如下:

<!DOCTYPE html>
<html>
    <meta charset="utf-8" />
    <head>
        <title></title>
    </head>
<body>
    <div id="header">
        <h1>ASP.NET MVC</h1>      
    </div>

    <div id="main">
        @RenderBody()
    </div>

    <div id="footer">
       <p>&copy; @DateTime.Now.Year - 我的ASP.NET项目</p>
    </div>
</body>
</html>

第三步,修改/View目录下Index.cshtml文件,去掉其他所有东西,只用下面一行:

<h1>Home Page</h1>

第四步,使用前文命令,重新编译项目,生成MyApplication.dll并发布,按照前文提到的需要发布的文件,即:

binMyApplication.dll
Views*
Global.asax
web.config

第五步,打开浏览器,查看运行结果,可以看到一样的展现页面,但View中已经去掉了HTML页面框架,只剩需要展示的内容。

二、增加Entity Framework

第一步,Entity Framework需要用到一下几个dll,你可以通过使用Nuget下载,也可以直接点这里下载:

EntityFramework.dll

MySql.Data.dll

MySql.Data.Entity.EF6.dll

笔者连接的数据库是MySql,如果你连接Sql Server,需要的dll就是EntityFramework.SqlServer.dll。将上面三个dll拷贝到bin目录

第二步,在根目录下创建Models目录,在里面建立MyDbContext.cs文件,内容如下:

 1 using System.Data.Entity;
 2 
 3 namespace MyApplication
 4 {
 5     [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
 6     public class MyDbContext : DbContext
 7     {
 8         public MyDbContext() : base("name=MyDbContext")
 9         {
10         }
11         public DbSet<User> Users { get; set; }
12     }
13     
14 }

MyDbContext是你web.config中连接字符串的名字,User是一会要建立的实体类,也就是数据库里的表名字,一定要和数据库里的一模一样,包括大小写都不能错。

第三步,建立实体类User,代码如下:

 1 using System;
 2 
 3 namespace MyApplication
 4 {
 5     public class User
 6     {
 7         public int Id { get; set; }
 8         public string Name { get; set; }
 9         public bool Age { get; set; }
10     }
11 }

上面User要和数据库中表明一样,Id,Name和Age也要和你数据库字段一样

第四步,修改HomeController.cs文件,内容如下(红色的是需要增加的部分):


using System.Web.Mvc;
using System.Data;
using System.Data.Entity; using System.Linq;

namespace
MyApplication { public class HomeController : Controller { private MyDbContext db = new MyDbContext(); public ActionResult Index() { User user = db.Users.SingleOrDefault(u => u.Id == 1); ViewBag.UserName = user.Name; return View(); } } }

第五步,在/Views/Home/Index.cshtml文件中任意地方,增加”@ViewBag.UserName“

第六步,修改web.config文件(浅青色是需要增加的部分):

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>

  <appSettings>
    <add key="webpages:Enabled" value="false"/>
  </appSettings>

  <system.web>
    <!--TODO: remove in production enviroment-->
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <!--TODO: remove in production enviroment-->
    <customErrors mode="Off"/>
  </system.web>

  <entityFramework>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>

  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient"/>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
    </DbProviderFactories>
  </system.data>

  <connectionStrings>
    <add name="MyDbContext" providerName="MySql.Data.MySqlClient" connectionString="Server=localhost;Uid=root;Pwd=123456;Database=test;Character Set=utf8;"/>
  </connectionStrings>
</configuration>

第七步,使用下列命令重新编译项目:

C:WindowsMicrosoft.NETFramework64v4.0.30319csc.exe /t:library /out:binMyApplication.dll /r:"binSystem.Web.Mvc.dll" /r:"binEntityFramework.dll" /r:"binMySql.Data.dll" /r:"binMySql.Data.Entity.EF6.dll" ControllersHomeController.cs Global.asax.cs App_StartRouteConfig.cs ModelsMyDbContext.cs ModelsUser.cs

第八步,发布代码,即可看到效果了。

好了,就写到这吧。很基础、无聊的东西,完全是为了”玩“,以后的内容你们自己扩展吧。

原文地址:https://www.cnblogs.com/andy65007/p/6870092.html