Developing an ASP. NET MVC application without Visual Studio (next article)

Source: Internet
Author: User
Tags connectionstrings

Book to the back!

The previous article, "purely handmade," completely discarded Visual Studio, made an ASP. NET MVC application, and worked well, and everything from project catalogs, source code, Web. config, and so on, had been "minimalist", removing a whole bunch of unrelated things that Visual Studio generated. , of course, this is just a "starting point", with the subsequent expansion of the project content and functionality will need to add a lot of things, but so clean a project, looking to let people comfortable, brick by brick of their own house, it is not a kind of enjoyment! (In fact, many people do not agree with this, in the original StackOverflow, the problem-solving people with "severe brain damage" (brain broken) to describe this practice)

The former is just a C # source code compilation Tool csc.exe compiled an ASP. NET MVC application, this article will add some very basic functions, the Entity framework and the master Page, these two are the development of general Web applications, the most basic things, One used to access the database, one to build the page frame, and a page frame, so that each controller's view is focused on what they want to achieve.

First, increase the master Page

The first step is to set up the file _viewstart.cshtml under directory/view, which is called each time the user accesses the view, with the following code:

@{    "~/views/shared/_layout.cshtml";}

The second step, create a folder shared, and inside the creation of the file _layout.cshtml, the contents are as follows:

<!DOCTYPE HTML><HTML>    <MetaCharSet= "Utf-8" />    <Head>        <title></title>    </Head><Body>    <DivID= "header">        <H1>ASP. NET MVC</H1>          </Div>    <DivID= "Main">@RenderBody ()</Div>    <DivID= "Footer">       <P>&copy;@DateTime. now.year-My ASP.</P>    </Div></Body></HTML>

The third step, modify the/view directory under the index.cshtml file, remove everything else, just use the following line:

< H1 > Home Page</H1>

The fourth step, using the previous command, recompile the project, generate MyApplication.dll and publish, according to the above mentioned need to publish the file, namely:

Bin\myapplication.dll
views\*
Global.asax
Web. config

The fifth step, open the browser, view the results of the operation, you can see the same display page, but the view has been removed from the HTML page frame, only the content needed to show.

Ii. Adding the Entity Framework

In the first step, the Entity framework needs to use a few DLLs, which you can download by using NuGet, or just click here to download:

EntityFramework.dll

MySql.Data.dll

MySql.Data.Entity.EF6.dll

The author connects the database is MySQL, if you connect SQL Server, need DLL is EntityFramework.SqlServer.dll. Copy the above three DLLs to the bin directory

In the second step, create the models directory under the root directory and build the MyDbContext.cs file inside it, with the following contents:

1 usingSystem.Data.Entity;2 3 namespaceMyApplication4 {5[Dbconfigurationtype (typeof(MySql.Data.Entity.MySqlEFConfiguration))]6      Public classMydbcontext:dbcontext7     {8          PublicMydbcontext ():Base("Name=mydbcontext")9         {Ten         } One          PublicDbset<user> Users {Get;Set; } A     } -      -}

Mydbcontext is the name of the connection string in your Web. config, user is the entity class to be created, that is, the name of the table in the database, must be exactly the same as the database, including the size of the case can not be wrong.

The third step is to create the entity class user with the following code:

1 usingSystem;2 3 namespaceMyApplication4 {5      Public classUser6     {7          Public intId {Get;Set; }8          Public stringName {Get;Set; }9          Public BOOLAge {Get;Set; }Ten     } One}

As shown in the database above, id,name and age are the same as your database fields.

The fourth step, modify the HomeController.cs file, the content is as follows ( Red is the need to add the section):

using SYSTEM.WEB.MVC;
Using System.Data;
Using system.data.entity;using System.Linq;

namespace myapplication{ publicclass homecontroller:controller { private Mydbcontext db = new Mydbcontext (); Public actionresult Index () {= db. Users.singleordefault (U = u.id = = 1); Viewbag.username = user. Name; return View ();}} }

Fifth step, add "@ViewBag. UserName" anywhere in the/views/home/index.cshtml file

Sixth step, modify the Web. config file ( light cyan is the part that needs to be added):

<?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>    <AddKey= "webpages:enabled"value= "false"/>  </appSettings>  <system.web>    <!--Todo:remove in production enviroment -    <compilationDebug= "true"targetframework= "4.5">      <Assemblies>        <AddAssembly= "SYSTEM.WEB.MVC, version=3.0.0.0, Culture=neutral, publickeytoken=31bf3856ad364e35"/>        <AddAssembly= "System.Web.WebPages, version=1.0.0.0, Culture=neutral, publickeytoken=31bf3856ad364e35"/>      </Assemblies>    </compilation>    <!--Todo:remove in production enviroment -    <customErrorsMode= "Off"/>  </system.web> <entityFramework> <providers> <provider invariantname=" mysql.data.m Ysqlclient "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 "Prov Idername= "MySql.Data.MySqlClient" connectionstring= "server=localhost; Uid=root; Pwd=123456;database=test; Character Set=utf8; " /> </connectionstrings> </Configuration>

Seventh step, recompile the project with the following command:

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

Eighth step, release the code, you can see the effect.

All right, let's just write about it. Very basic, boring things, completely to "play", the future content of your own expansion bar.

Developing an ASP. NET MVC application without Visual Studio (next article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.