webapi-1 给现有MVC 项目添加 WebAPI
1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http
2. 在App_Start 下创建 WebApiConfig.cs 并注册路由
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Http;
- namespace Libaray.Web.App_Start
- {
- public static class WebApiConfig
- {
- public static void Register(HttpConfiguration config)
- {
- // Web API 配置和服务
- // Web API 路由
- config.MapHttpAttributeRoutes();
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- }
- }
- }
3. 在Global.asax, Application_Start 下添加 WebAPI 配置
- using Libaray.Web.App_Start;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using System.Web.Mvc;
- using System.Web.Optimization;
- using System.Web.Routing;
- namespace Libaray.Web
- {
- public class MvcApplication : System.Web.HttpApplication
- {
- protected void Application_Start()
- {
- AreaRegistration.RegisterAllAreas();
- GlobalConfiguration.Configure(WebApiConfig.Register);
- FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
- RouteConfig.RegisterRoutes(RouteTable.Routes);
- BundleConfig.RegisterBundles(BundleTable.Bundles);
- }
- }
- }
4. 在第一步添加的WebApi 中填写相应代码,
- using Libaray.Web.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace Libaray.Web.Controllers
- {
- [RoutePrefix("api/SystemUsers")]
- public class SystemUsersController : ApiController
- {
- [HttpGet, Route("GetUserList")]
- public List<UserModel> GetUserModels()
- {
- UserModelService UserBS = new UserModelService();
- return UserBS.FindList(u => u.isActive == true);
- }
- [HttpGet, Route("GetUser")]
- public UserModel GetUserModel(int id = )
- {
- if(id != )
- {
- UserModelService UserBS = new UserModelService();
- return UserBS.Find(u => u.Id == id);
- }
- else
- {
- return null;
- }
- }
- [HttpPost, Route("Login")]
- public bool Login(string loginId,string password)
- {
- UserModelService UserBS = new UserModelService();
- return UserBS.ValidateLoginInfo(loginId, password);
- }
- }
- }
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的更多相关文章
- 给现有MVC 项目添加 WebAPI
1. 增加一个WebApi Controller, VS 会自动添加相关的引用,主要有System.Web.Http,System.Web.Http.WebHost,System.Net.Http 2 ...
- Asp.Net Mvc项目添加WebApi
1.添加一个WebApi 空项目 2.删除WebApi项目下的 Global.asax 文件,因为我们要把WebApi项目整合到Mvc项目中去,全局只需要一个Global 3.修改 WebApi 项目 ...
- 当重装eclipse后,给现有web项目添加tomcat的构建路径
在eclipse“首选项”-“service environment”中配置好tomcat后,给现有web项目添加构建路径: 1.选中一个web项目右键选中“构建路径”-“配置构建路径”
- MVC项目加入WebApi
一.NuGet搜索安装Microsoft.AspNet.WebApi,注意引用的版本依赖,因为是在完整的MVC项目上新增,在本地编译调试并没有报错,发布到IIS后却显示应用程序出错. 二.NuGet搜 ...
- ASP.NET MVC4空MVC项目添加脚本压缩和合并
本文介绍的是 建立的空MVC项目如何添加该功能 1.选中MVC项目,右键>"管理解决反感的NuGet程序包" 2.在"联机"中在线搜索搜索"Op ...
- asp.net mvc项目创建WebApi简单例子
1.创建默认路由的映射. namespace RedisDemo.App_Start { public class WebApiConfig { public static void Register ...
- Net项目添加 WebAPI
1.新建一个 WebApiConfig.cs public static void Register(HttpConfiguration config) { // Web API 配置和服务 // ...
- 如何将现有的项目添加到远程的git库里面!
我们经常都会遇到这样的场景,就是将本地的一个项目同步到网上远程的git库里面.最近也遇到这样的问题,发现网上很少人讲到这个问题,但是这个问题是很多程序员遇到的版本库管理的最早的拦路虎. 我的远程是ht ...
- 给现有MVC项目增加Web API支持
在MVC4中自带了Web API不再需要从Nuget中下载. Step1:增加System.Web.Http,System.Web.Http.WebHost,System.Net.Http三个程序集的 ...
随机推荐
- wsgiref 源码解析
Web Server Gateway Interface(wsgi),即Web服务器网关接口,是Web服务器软件和用Python编写的Web应用程序之间的标准接口. 想了解更多关于WSGI请前往: h ...
- POJ2115:C Looooops——题解
http://poj.org/problem?id=2115 题目大意:for(i=A;i!=B;i+=C),i的类型的范围为0<=a<1<<k exgcd裸题目. 设a=C, ...
- BZOJ2049:[SDOI2008]洞穴勘测——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2049 https://www.luogu.org/problemnew/show/P2147 辉辉热 ...
- BZOJ2152:聪聪可可——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2152 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一 ...
- [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 ...
- Redux的应该注意的问题
1. Store中的State修改不能直接修改原有的State,若直接修改State,则redux中的所有操作都将指向 内存中的同一个state,将无法获取每一次操作前后的state,就无法追溯sta ...
- sass的颜色函数
sass中有些非常实用的颜色处理函数,总结如下 1.颜色加深或变浅 lighten($color,$amount) //颜色变浅 darken($color,$amount) //颜色加深 例如: l ...
- HDU 5640
King's Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法?
参考:http://blog.csdn.net/mazhimazh/article/details/16799925 1. Java八种基本数据类型的大小,以及封装类,自动装箱/拆箱的用法? 原始类型 ...
- LoaderManager与CursorLoader用法
一.基本概念 1.LoaderManager LoaderManager用来负责管理与Activity或者Fragment联系起来的一个或多个Loaders对象. 每个Activity或者Fragme ...