1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http

2. 在App_Start 下创建 WebApiConfig.cs 并注册路由

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web.Http;
  7.  
  8. namespace Libaray.Web.App_Start
  9. {
  10. public static class WebApiConfig
  11. {
  12. public static void Register(HttpConfiguration config)
  13. {
  14. // Web API 配置和服务
  15.  
  16. // Web API 路由
  17. config.MapHttpAttributeRoutes();
  18.  
  19. config.Routes.MapHttpRoute(
  20. name: "DefaultApi",
  21. routeTemplate: "api/{controller}/{id}",
  22. defaults: new { id = RouteParameter.Optional }
  23. );
  24. }
  25. }
  26. }

3. 在Global.asax, Application_Start 下添加 WebAPI 配置

  1. using Libaray.Web.App_Start;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Http;
  7. using System.Web.Mvc;
  8. using System.Web.Optimization;
  9. using System.Web.Routing;
  10.  
  11. namespace Libaray.Web
  12. {
  13. public class MvcApplication : System.Web.HttpApplication
  14. {
  15. protected void Application_Start()
  16. {
  17. AreaRegistration.RegisterAllAreas();
  18. GlobalConfiguration.Configure(WebApiConfig.Register);
  19. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  20. RouteConfig.RegisterRoutes(RouteTable.Routes);
  21. BundleConfig.RegisterBundles(BundleTable.Bundles);
  22. }
  23. }
  24. }

4. 在第一步添加的WebApi 中填写相应代码,

  1. using Libaray.Web.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Web.Http;
  8.  
  9. namespace Libaray.Web.Controllers
  10. {
  11. [RoutePrefix("api/SystemUsers")]
  12. public class SystemUsersController : ApiController
  13. {
  14. [HttpGet, Route("GetUserList")]
  15. public List<UserModel> GetUserModels()
  16. {
  17. UserModelService UserBS = new UserModelService();
  18. return UserBS.FindList(u => u.isActive == true);
  19. }
  20.  
  21. [HttpGet, Route("GetUser")]
  22. public UserModel GetUserModel(int id = )
  23. {
  24. if(id != )
  25. {
  26. UserModelService UserBS = new UserModelService();
  27. return UserBS.Find(u => u.Id == id);
  28. }
  29. else
  30. {
  31. return null;
  32. }
  33. }
  34.  
  35. [HttpPost, Route("Login")]
  36. public bool Login(string loginId,string password)
  37. {
  38. UserModelService UserBS = new UserModelService();
  39. return UserBS.ValidateLoginInfo(loginId, password);
  40. }
  41. }
  42. }

5. Run the application and call the API. Example: http://localhost:49919/api/SystemUsers/GetUserList

6. 一些疑问

做完以上配置后,运行网页,仍不能得到想要的结果,后查出因为我是在Areas里面建立了一个API的文件夹

只要将View文件夹和APIAreaRegistration.cs文件删除,问题就消失了,具体原因没有细究,应该还是路由的问题。

引用:

https://www.cnblogs.com/tuyile006/p/6151555.html

webapi-1 给现有MVC 项目添加 WebAPI的更多相关文章

  1. 给现有MVC 项目添加 WebAPI

    1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ...

  2. Asp.Net Mvc项目添加WebApi

    1.添加一个WebApi 空项目 2.删除WebApi项目下的 Global.asax 文件,因为我们要把WebApi项目整合到Mvc项目中去,全局只需要一个Global 3.修改 WebApi 项目 ...

  3. 当重装eclipse后,给现有web项目添加tomcat的构建路径

    在eclipse“首选项”-“service environment”中配置好tomcat后,给现有web项目添加构建路径: 1.选中一个web项目右键选中“构建路径”-“配置构建路径”

  4. MVC项目加入WebApi

    一.NuGet搜索安装Microsoft.AspNet.WebApi,注意引用的版本依赖,因为是在完整的MVC项目上新增,在本地编译调试并没有报错,发布到IIS后却显示应用程序出错. 二.NuGet搜 ...

  5. ASP.NET MVC4空MVC项目添加脚本压缩和合并

    本文介绍的是 建立的空MVC项目如何添加该功能 1.选中MVC项目,右键>"管理解决反感的NuGet程序包" 2.在"联机"中在线搜索搜索"Op ...

  6. asp.net mvc项目创建WebApi简单例子

    1.创建默认路由的映射. namespace RedisDemo.App_Start { public class WebApiConfig { public static void Register ...

  7. Net项目添加 WebAPI

    1.新建一个  WebApiConfig.cs public static void Register(HttpConfiguration config) { // Web API 配置和服务 // ...

  8. 如何将现有的项目添加到远程的git库里面!

    我们经常都会遇到这样的场景,就是将本地的一个项目同步到网上远程的git库里面.最近也遇到这样的问题,发现网上很少人讲到这个问题,但是这个问题是很多程序员遇到的版本库管理的最早的拦路虎. 我的远程是ht ...

  9. 给现有MVC项目增加Web API支持

    在MVC4中自带了Web API不再需要从Nuget中下载. Step1:增加System.Web.Http,System.Web.Http.WebHost,System.Net.Http三个程序集的 ...

随机推荐

  1. wsgiref 源码解析

    Web Server Gateway Interface(wsgi),即Web服务器网关接口,是Web服务器软件和用Python编写的Web应用程序之间的标准接口. 想了解更多关于WSGI请前往: h ...

  2. POJ2115:C Looooops——题解

    http://poj.org/problem?id=2115 题目大意:for(i=A;i!=B;i+=C),i的类型的范围为0<=a<1<<k exgcd裸题目. 设a=C, ...

  3. BZOJ2049:[SDOI2008]洞穴勘测——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2049 https://www.luogu.org/problemnew/show/P2147 辉辉热 ...

  4. BZOJ2152:聪聪可可——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2152 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一 ...

  5. [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:  You may assume th ...

  6. Redux的应该注意的问题

    1. Store中的State修改不能直接修改原有的State,若直接修改State,则redux中的所有操作都将指向 内存中的同一个state,将无法获取每一次操作前后的state,就无法追溯sta ...

  7. sass的颜色函数

    sass中有些非常实用的颜色处理函数,总结如下 1.颜色加深或变浅 lighten($color,$amount) //颜色变浅 darken($color,$amount) //颜色加深 例如: l ...

  8. HDU 5640

    King's Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  9. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?

    参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...

  10. LoaderManager与CursorLoader用法

    一.基本概念 1.LoaderManager LoaderManager用来负责管理与Activity或者Fragment联系起来的一个或多个Loaders对象. 每个Activity或者Fragme ...