Aside from using Web APIs in MVC4, The mvc4webapi

Source: Internet
Author: User

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

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.