不使用Visual Studio開發ASP.NET MVC應用(下篇)

來源:互聯網
上載者:User

標籤:ble   ast   構建   4.0   targe   題解   cte   tor   UI   

書接上回!

前文“純手工”、徹底拋棄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並發布,按照前文提到的需要發布的檔案,即:

bin\MyApplication.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:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe /t:library /out:bin\MyApplication.dll /r:"bin\System.Web.Mvc.dll" /r:"bin\EntityFramework.dll" /r:"bin\MySql.Data.dll" /r:"bin\MySql.Data.Entity.EF6.dll" Controllers\HomeController.cs Global.asax.cs App_Start\RouteConfig.cs Models\MyDbContext.cs Models\User.cs

第八步,發布代碼,即可看到效果了。

好了,就寫到這吧。很基礎、無聊的東西,完全是為了”玩“,以後的內容你們自己擴充吧。

不使用Visual Studio開發ASP.NET MVC應用(下篇)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.