How to call / link to external web api project in MVC4 project


I am new to web API and MVC. I created a new WEB API and MVC solution separately. Now I want to reference the web API action method in MVC, so for this next code I wrote,
Web Api Project Side,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Data;
using System.Net.Http;
using System.Web.Http;
using AttributeRouting.Web.Mvc;
using RegisterStudent_WebAPI.Models;

    namespace Register_Student_WebAPI.Controllers 
    {
        public class RegisterStudentController : ApiController
        {
            [Route("api/student")]
            [HttpGet]
            public IEnumerable<Student> GetStudents()
            {
                RegisterStudent_API_DB objRegisterStudent = new RegisterStudent_API_DB();
                List<Student> lstStudent = objRegisterStudent.GetStudent();
                return lstStudent ;
            }
        } }

WEB.Config file from API,

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


namespace RegisterStudent_WebAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {


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

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();

            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api
            config.EnableSystemDiagnosticsTracing();


            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
        }
    }
}


in MVC Project, I wrote the following code in a script tag (in the download form) to link to the WEB API service,

$(document).ready(function () {   
        jQuery.support.cors = true;
        $.ajax({
            url: 'http://localhost:18715/api/student',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                alert('success');
            },
            error: function (x) {
                alert(x.status);
            }
        });
    });

If I add a link to the web API project to the MVC project, then it works fine, but one of my friends said that services should not be mentioned that way. Please direct me how to link / enable hosting / cross domain running api web project, my MVC project?

+4
source share
1 answer

, , ( - ;)), , WebAPI MVC, , API /.

, WebAPI ( , OWIN/webservice/ API), :

  • MVC
  • /

WebAPI - RESTful, API, .

, WebAPI MVC, , . , , JsonResult MVC. , API.

0

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


All Articles