Aside from using Web APIs in MVC4, The mvc4webapi
In the previous blog post on WebAPI usage, I introduced the sample Web API used in MVC4. However, sometimes we just want to use the Web API function, instead of using the entire MVC. In this case, we should leave MVC4 alone to create a project.
First, create a new asp.net empty application, and add reference System. Web. Http and System. Web. Http. WebHost to the program:
Add System. Net. Http
You also need to reference Json.net. You can use the downloaded dll through Nuget or directly.
Add route ing
This step is the same as in the previous article. We can take the configuration of the previous Article directly:
public class WebApiConfig{ public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }}
Create a Global. asax file and complete registration in Application_Start.
protected void Application_Start(object sender, EventArgs e){ WebApiConfig.Register(GlobalConfiguration.Configuration);}
Create a Web API Controller
Add UserModel to the project first.
public class UserModel{ public string UserID { get; set; } public string UserName { get; set; }}
Create an API directory in the project and take the UserController in the previous article directly.
public class UserController : ApiController{ public UserModel getAdmin() { return new UserModel() { UserID = "000", UserName = "Admin" }; } public bool add(UserModel user) { return user != null; }}
Run the test program in the previous article.
Reference page: http://qingqingquege.cnblogs.com/p/5933752.html