How to create a web API using MVC 3.0

I am using Visual Studio 2010 with the .NET Framework 4 and ASP.NET MVC 3.

I want my controller method to be available for external applications such as (websites, mobile applications, etc.) as a web API. I tried to find a solution on the Internet, but got all the links pointing to the "Web API2 Controllers" from VS 2013, for example. this one . I cannot update Visual Studio and .net at this point.

Is there a way I can achieve this using .NET 4 and ASP.NET MVC 3?

+4
source share
2 answers

To get the web API in the MVC3 project, make the following changes:

1. MVC3 .Net 4.

2 - -API - Microsoft.AspNet.WebApi -Version 4.0.30506

3. Global.asax using System.Web.Http; -

routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
);

4. .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class ValuesController : ApiController
    {

        public string Get()
        {
            return "1";
        }

    }
}

5. URL- - /api/values ​​. , -API.

+9
+2

Source: https://habr.com/ru/post/1524993/


All Articles