给现有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 = 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

时间: 2024-08-26 09:09:21

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

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.Ta

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

在eclipse"首选项"-"service environment"中配置好tomcat后,给现有web项目添加构建路径: 1.选中一个web项目右键选中"构建路径"-"配置构建路径" 2.选中右边"add library" 3.选择"service runtime"然后点击"下一步" 4.选择正确版本点击"完成"就可以了.

.NET开发 MVC项目 添加Controller出现的错误:无法检索到元数据

ERROR: ---------------------------Microsoft Visual Studio---------------------------无法检索"MvcMusicStore.Models.Album"的元数据.Using the same DbCompiledModel to create contexts against different types of database servers is not supported. Instead, cre

MVC.Net:WebAPI添加对jsonP的支持

在某些情况下,我们需要在WebAPI项目中添加对jsonP的支持.比如我们同时创建了MVC.Net和WebAPI两个项目,这两个项目使用不同的端口,这时如果MVC.Net项目的前端想要直接访问WebAPI项目,就会出现"cross-origin"的错误.要解决这个问题,我们通常会使用jsonP的方法. 要让WebAPI支持返回jsonP格式,需要的步骤如下: 1. 通过NuGet安装WebApiContrib.Formatting.Jsonp.项目地址:https://github.c

添加Bootstrap 到MVC项目中(vs2010)

环境:Visual Studio 2010   ASP.NET MVC3 手动添加: 下载地址:http://twitter.github.com/bootstrap/ 一个js,两个css,两张图 js 文件:  •bootstrap.min.js CSS 文件:  •bootstrap.min.css  •bootstrap-responsive.min.css image 文件:  •glyphicons-halflings.png  •glyphicons-halflings-white

1、新建MVC项目,添加easyui,并启用捆绑压缩

一.新建MVC项目,选择mvc4,选择.NET Framework4框架.考虑对server 2003的支持,选择.NET Framework4框架. 二.选择项目模板:选"空",视图引擎:选"Razor". 三.添加easy ui 1.右键选择"管理NuGet程序包" 2.在联机中搜索easyui ,此处为1.4.5版本,点击安装.安装完成后项目中自动新增"Content"和"script"两个文件夹.

MVC.Net:MVC.Net与WebAPI的IOC实现

我们通过Ninject(http://www.ninject.org/)项目来帮助我们实现MVC.Net与WebAPI项目的IOC实现. 首先我们来看MVC.Net的IOC实现. 1. 通过NuGet我们安装Ninject.MVC3,它会同时安装Ninject.Ninject.Web.Common和Ninject.Web.Common.WebHost.不用被它的名字干扰,它对MVC.Net 5也是适用的. 2. 安装成功后,在MVC.Net的App_Start目录中会多出一个NinjectWeb

使用Areas分离ASP.NET MVC项目

为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际的开发中,随着项目规模的不断扩大,Controller控制器也随之不断增多.如果在Controllers文件夹下面有超过两位数controller,即便采用良好的命名规范,或者用子文件夹的形式区分不同功能的控制器,还是会影响项目的可阅读性和可维护性.因此,在一些场景下,如果能把与某功能相关的文件分离

MVC项目插件化改造

现在正在做的项目是以长沙某个客户的需求为原型进行开发的,随着业务的拓展,其他城市相同行业的客户也有类似的需求,在进行投标时,每个客户都有自己的页面和功能要求,如果单纯用长沙客户的软件版本,无法完全满足标书要求,因此需要对项目进行改造,考虑采用MVC插件化方式. 现有项目使用的技术点主要是MVC+EF+jQuery,改造思路考虑将每一个功能模块抽取为一个插件,在主Web项目中引用各插件,并将插件的css.js.views等文件拷贝到主Web项目的Plugins文件夹下.当有新客户提出个性化需求时,