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 = 0)
{
if(id != 0)
{
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

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

  1. 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. 如何将现有的项目添加到远程的git库里面!

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

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

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

  8. Net项目添加 WebAPI

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

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

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

随机推荐

  1. linq查询xml

    1.加载xml字符串 XElement root = XElement.Parse(@"<?xml version='1.0' encoding='utf-8'?> <It ...

  2. EntityFramework中几种更改数据的方式

    首先声明个实体类,该实体类是EntityFrameWork自动生成的,对应数据表Test结构如下 public partial class Test { public int Id{ get; set ...

  3. EL表达式错误attribute items does not accept any expressions

    解决方法是将 <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%> 改成 <% ...

  4. Scalaz(9)- typeclass:checking instance abiding the laws

    在前几篇关于Functor和Applilcative typeclass的讨论中我们自定义了一个类型Configure,Configure类型的定义是这样的: case class Configure ...

  5. tp5页面输出时,搜索后跳转下一页的处理

    tp5页面输出时,搜索功能在跳转下一页时,如果不做任何处理,会返回原有是第二页输出的数据.为了保证跳转下一页时输出的是搜索到的数据,做以下处理. (要根据自己的搜索字段进行适当修改) 页面js代码,给 ...

  6. <welcome-file-list>标签的控制作用以及在springmvc中此标签的的配置方式

    我们在写安全性较高的网站时必然会对网站的入口进行限制, 而在这其中其关键作用的就是网站的根目录下WEB-INF中的web.xml中<welcome-file-list>  <welc ...

  7. mybatis 3的TypeHandler解析(null值的处理)

    最近,在测试迁移公司的交易客户端连接到自主研发的中间件时,调用DAO层时,发现有些参数并没有传递,而在mapper里面是通过parameterMap传递的,因为有些参数为null,这就导致了参数传递到 ...

  8. 12款免费的 WordPress 响应式主题下载

    最流行的内容管理系统(WordPress)和最流行的网页设计技术(响应式设计)结合会是什么样的呢?下面这个列表收集了12款响应式的 Wordpress 主题,能够带给你不一样的网站体验. 您可能感兴趣 ...

  9. FormsAuthentication身份认证源代码

    使用FormsAuthentication类可以实现身份认证功能,这里提供一个asp.net项目的源代码,项目名称KWS.项目实现了登录.退出和判断身份的功能. 关于项目 点击这里下载源代码 http ...

  10. CSS学习总结(三)

    一.属性选择符 如下表所示: 例子如下: <head> <meta charset="utf-8"> <style type="text/c ...