不使用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>© @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文件,内容如下:
using System.Data.Entity; namespace MyApplication
{
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class MyDbContext : DbContext
{
public MyDbContext() : base("name=MyDbContext")
{
}
public DbSet<User> Users { get; set; }
} }
MyDbContext是你web.config中连接字符串的名字,User是一会要建立的实体类,也就是数据库里的表名字,一定要和数据库里的一模一样,包括大小写都不能错。
第三步,建立实体类User,代码如下:
using System; namespace MyApplication
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public bool Age { get; set; }
}
}
上面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应用(下篇)的更多相关文章
- 不使用Visual Studio开发ASP.NET MVC应用(上篇)
入行十多年,工作闲暇,还是对信息技术比较关注,经常测试一些新的技术,感受一下科技发展给大家带来的便利.Visual Studio接触也有年头了,对它总感觉乎近乎远的,既熟悉又陌生,一直没有像用别的工具 ...
- Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程
原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...
- Visual Studio 2017 ASP.NET Core开发
Visual Studio 2017 ASP.NET Core开发,Visual Studio 2017 已经内置ASP.NET Core 开发工具. 在选择.NET Core 功能安装以后就可以进行 ...
- ASP.NET Core 中文文档 第二章 指南(2)用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API
原文:Building Your First Web API with ASP.NET Core MVC and Visual Studio 作者:Mike Wasson 和 Rick Anderso ...
- ASP.NET安全[开发ASP.NET MVC应用程序时值得注意的安全问题](转)
概述 安全在web领域是一个永远都不会过时的话题,今天我们就来看一看一些在开发ASP.NET MVC应用程序时一些值得我们注意的安全问题.本篇主要包括以下几个内容 : 认证 授权 XSS跨站脚本攻击 ...
- 带你使用Visual Studio 2019创建一个MVC Web应用
工欲善其事必先利其器,我们既然有Visual Studio2019这样的IDE为什么不用?学.Net Core而不用Visual Studio进行开发可谓是多么另类呀!既然你已经安装了VS2019的话 ...
- Visual Studio开发首选!DevExtreme v19.1.6全新来袭
DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...
- 【转载】保哥 釐清 CLR、.NET、C#、Visual Studio、ASP.NET 各版本之間的關係
我常常不仅仅逛 博客园,还会去找国外,特别是台湾的技术部落格,发现好的文章,我便会收录,今天我转载或者全文复制,在Google 博客园,一位叫保哥, 釐清 CLR..NET.C#.Visual Stu ...
- 用于 Visual Studio 和 ASP.NET 的 Web 应用程序项目部署常见问题
https://msdn.microsoft.com/zh-cn/library/ee942158(v=vs.110).aspx#can_i_exclude_specific_files_or_fol ...
随机推荐
- selenium自动化定位方式
自动化定位方式 1.String Xpath = String.format("//*[@id=\"saveFileKeyWordsBtnHand\"]/../../.. ...
- MPI编程——分块矩阵乘法(cannon算法)
https://blog.csdn.net/a429367172/article/details/88933877
- Underscore源码阅读极简版入门
看了网上的一些资料,发现大家都写得太复杂,让新手难以入门.于是写了这个极简版的Underscore源码阅读. 源码: https://github.com/hanzichi/underscore-an ...
- ImageWatch 无法安装在VS2017环境下的解决方案
Download: https://marketplace.visualstudio.com/items?itemName=VisualCPPTeam.ImageWatch#qna For Visua ...
- python 两个 list 获取交集,并集,差集的函数
1. 获取两个 list 的交集 a = [1, 2, 3, 4] b = [1, 2, 5] print(list(set(a).intersection(set(b)))) 2. 获取两个 lis ...
- 20175212课下作业 MyCP
20175212课下作业 MyCP 要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2 ...
- 《用Python写爬虫》学习笔记(一)
注:纯文本内容,代码独立另写,属于本人学习总结,无任何商业用途,在此分享,如有错误,还望指教. 1.为什么需要爬虫? 答:目前网络API未完全放开,所以需要网络爬虫知识. 2.爬虫的合法性? 答:爬虫 ...
- Mysql 了解changeBuffer 与 purge 调优
需要删除.新增记录或更新一个数据页时,如果数据页在内存中就直接更新,而如果这个数据页还没有在内存中的话,在不影响数据一致性的前提下,InooDB 会将这些更新操作缓存在 change buffer中, ...
- C# [GDI+] [API] Get Image bytes Length
MemoryBMP "{b96b3caa-0728-11d3-9d7b-0000f81ef32e}" 2 Bmp "{b96b3cab-0728-11d3-9d7b-00 ...
- crt证书iis 中引用 程序目录提示 System.UnauthorizedAccessException:拒绝访问
在站点根目录添加 Authenticated Users 权限