c# – 如何在MVC4项目中调用/引用外部web api项目
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何在MVC4项目中调用/引用外部web api项目,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2819字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何在MVC4项目中调用/引用外部web api项目](/upload/InfoBanner/zyjiaocheng/766/e23a20c5f2094774a327bbe35f7188d6.jpg)
我是Web API&的新手. MVC我创建了新的WEB API& MVC解决方案单独现在我想在MVC中引用Web API动作方法,所以对于我写的下面的代码,
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 ;
}
} }
来自API的WEB.Config文件,
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"));
}
}
}
在MVC项目中我已经在脚本标签(在加载表单上)编写了以下代码来引用WEB API服务,
$(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);
}
});
});
如果我将Web API项目的引用添加到MVC项目然后它工作正常但我的一个朋友告诉服务不应该被这样引用,请指导我如何引用/包含运行web api项目的托管/跨域项目到我的MVC项目?
解决方法:
你所做的一切都不错(至少 – 它不被禁止;))但是通过直接在MVC项目中引用WebAPI,你将它绑定到一个特定的应用程序,什么将阻止你在其他项目/服务中重用API .
在我看来,WebAPI项目应该作为另一个应用程序托管(它取决于你是否应该是一个自托管的OWIN应用程序/ webservice / simple API应用程序),它给你一些好处:
>它与您的MVC应用程序逻辑分开
>它可以单独部署
>它可以被其他应用程序/服务使用
>它更容易维护
将WebAPI视为RESTful Web服务,可以由应用程序或第三方应用程序使用的API.
老实说,直接在MVC中使用WebAPI只是为了提供你的观点,一些数据似乎有点浪费.由于MVC控制器中的JsonResult操作,您正在做的事情可以完成.它并不理想,但会阻止您创建另一个API项目.
内容总结
以上是互联网集市为您收集整理的c# – 如何在MVC4项目中调用/引用外部web api项目全部内容,希望文章能够帮你解决c# – 如何在MVC4项目中调用/引用外部web api项目所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。