[Go]vs2012 + Web API + OData + EF + MYsql development and deployment

Source: Internet
Author: User
Tags app service

This article transferred from: http://www.cnblogs.com/liumang/p/4403436.html

First of all, my case, b/s development This piece has been a long time no, what Web API, MVC, OData are just heard, no actual development, because recently to develop a mobile app service, so prepare to use this framework to develop.

The following starts to get to the point (I try to write myself to start writing all the problems encountered in the restore, in order to remind myself later, the article started after I developed the deployment has been written, so some places are missing, please forgive me)

1: First create a new ASP. NET empty Web application with vs2012(note that I chose the.

2: This time the project should be a blank piece, nothing. First we'll add a few DLLs to the reference "System.web.http,system.web.http.webhost,system.net.http,newtonsoft.json" in the project reference

In addition to Newtonsoft.json, the other three DLLs can be found directly in the reference, Newtonsoft.json need to use the "Package Manager console" to load, command the following "Install-package Newtonsoft.json", Case insensitive

3: Create the model, controller file, in the project location of their own definition, the file code content is as follows:

    Class User    {        setset;}}   
 public class        Usercontroller:apicontroller {public User Get () {return new User () {UserID =  "000", UserName = admin "};} public bool add (User User) {return user! = null  

(The above code is I find the demo on the Internet search, not their original, we forgive)

3: Create a routing rule (this should be a knowledge of MVC, just created by vs automatically, and now handwritten by yourself)

Define a class

Class webapiconfig{        void"defaultapi""Api/{controller}/{id}"new {id = routeparameter.optional}); } }

Then manually add a global file yourself and add the following code

void Application_Start (object sender, EventArgs e) {    webapiconfig.register ( globalconfiguration.configuration);} 

Ok, so far, a Web API program created by VS2012 has been completed.

Launch vs, enter "localhost: port number/api/user" in the browser to access the

(The above code refers to the network information, please forgive me for forgetting the original address.)

All the way up to the top of the code, are very smooth, at least I did not encounter problems in the development process, the following start, there will be a variety of problems.

Start adding support for OData now and use NuGet to add support for OData

, there will be two choices, one for v1-3 and one for the V4 version. Here I chose V1-3, because I heard that the V4 version seems to be not very stable.

(Speaking of the problems I encountered here, when I first created the project, I chose the. NET 4.0 version, if you choose the 4.5 default version, add the OData v4 version will prompt for 4.5 unsupported information, if you add the OData v1-3 version, after the successful addition, run the project,

This time the project will be error, please forgive me Caishuxueqian, did not find out what is the reason to give up. Because I found that using the. NET 4.0 version of the OData V1-3 project is not an error, of course, this is also I have tried a lot of solutions to solve the problem)

After adding the OData component, you need to make some modifications in the project to modify the Usercontroller code for the controller file as follows:

[Queryable]  Get() {    "Admin" }}. AsQueryable ();}       

OK, it is so simple, now you can browse the data in the browser, I only do a piece of data here, so the view does not see what effect

If you make a few more data, you can access it correctly using the following URL:

localhost: Port/api/user? $top 2

localhost: Port/api/user? $filter =userid EQ ' 000 '

Be aware that OData is case-sensitive.

OK, let's start with EF to add support for MySQL.

First you need to download the installation components: "Mysql-connector-net-6.6.7.msi", this download on the official website is good, but to register, a little trouble.

Then install EF, use the Package Manager console to load, command the following "Install-package EntityFramework",

After installation, restart VS, select "New Item" in the project

Select, fill in the name, click Add

Here are some versions of the choice will be different, I only have these two options, select "Generate From Database", click Next

Here to fill in your MySQL IP, user name, password OK.

(When installing "Mysql-connector-net-6.6.7.msi", if your development machine installed MySQL software, there may be some problems, usually in "programs and features" in the original deleted,

Install the new, or you will choose the data source, there is no "Mysql database" this option)

A "*.edmx" file appears in the project, which is the file created by EF

At this point, create a new controller file, you name it, the code is as follows:

Class Productcontroller:apicontroller    {        "ID", allowedqueryoptions = PublicIQueryable <map_goodsdiscount>Newreturn Db.map_goodsdiscount. AsQueryable (); } }

which

Map_goodsdiscount is the name of your data table in MySQL,
The allowedorderbyproperties in the Queryable property indicates the default sort by the ID field
The Allowedqueryoptions property controls OData query conditions, such as

So far, all the code has been written, and if anything is missing, I'll add it when I find it.
Starting with deployment, the deployment environment for Windows Server R2, the 4.0 environment is installed, and here's a list of considerations for Deployment 1: The version of EF in the project, The default is 4.5, the deployment needs to remove the EntityFramework reference, and Add 4.0 version of the DLL, my native 4.0 address is:
 <section name= "entityframework "Type="  "Requirepermission= "false"/>      
<section name="entityframework"type= " System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, version=4.4.0.0, Culture=neutral, publickeytoken=b77a5c561934e089"requirepermission="false"/>      

2: If you encounter a 404 error while deploying, add the following code in the Web. config

<system.webServer> <validation validateintegratedmodeconfiguration="False"/> <modules runallmanagedmodulesforallrequests="True"/> "extensionlessurlhandler-integrated-4.0"/> <add name="extensionlessurlhandler-integrated-4.0 "Path= "*.  "Verb= * "Type=" system.web.handlers.transferrequesthandler  "Resourcetype="  Unspecified "Requireaccess=" script "integratedmode,runtimeversionv4.0"/> </ Handlers> </system.webserver>           

All the time, my project from development to deployment has been completed, which has a lot of problems, are online looking for information, coupled with their own a little bit of experience to solve, I hope to other people will have a little help.

[Go]vs2012 + Web API + OData + EF + MYsql development and deployment

Related 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.