webapi-1 给现有MVC 项目添加 WebAPI

摘要:
1.添加WebApiController,VS会自动添加相关引用,主要包括System.Web。Http,System.Web.Http。WebHost,System.Net。Http2_创建WebApiConfig。cs,并使用System注册路由;使用System.Collections。通用的使用系统。Linq;使用Syste

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

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

webapi-1 给现有MVC 项目添加 WebAPI第1张webapi-1 给现有MVC 项目添加 WebAPI第2张
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 }
            );
        }
    }
}
View Code

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

webapi-1 给现有MVC 项目添加 WebAPI第1张webapi-1 给现有MVC 项目添加 WebAPI第4张
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);
        }
    }
}
View Code

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

webapi-1 给现有MVC 项目添加 WebAPI第1张webapi-1 给现有MVC 项目添加 WebAPI第6张
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);
        }
    }
}
View Code

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

6. 一些疑问

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

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

webapi-1 给现有MVC 项目添加 WebAPI第7张

引用:

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

免责声明:文章转载自《webapi-1 给现有MVC 项目添加 WebAPI》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇数据结构习题--栈与队列(2)apache benchmark 的简单安装与测试下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

java 类变量初始化顺序

假定有一个类定义如下: package com.zhang; public final class Girl { // static代码块1 private static String sex = "female"; // 成员方法代码块1 private String name = "anonymous";...

C#窗体如何通过keybd_event()函数模拟键盘按键(组合键)产生事件

如何模拟键盘按键触发产生的事件,比如模拟按下Alt + F4 关闭当前程序,Ctrl+Shift 切换输入法等 可以通过win32api 键盘事件 keybd_event() 来实现 1、定义键盘按键对应得键码 #region bVk参数 常量定义 public const byte vbKeyLButton = 0x1;...

台式机安装CentOS7.6 Minimal ISO系统并增加图形化桌面

需求:公司测试环境因业务原因,需要在台式电脑上安装带桌面的CentOS系统,因同事有一个7.6版本Minimal ISO镜像的安装U盘,为了图方便没有去下载everything ISO镜像,而是待同事安装好最小化系统后我来添加桌面。系统安装参考如下链接:https://jingyan.baidu.com/article/59703552b61eb78fc0...

vim recording功能介绍

使用vim时无意间触碰到q键,左下角出现“recording”这个标识,觉得好奇,遂在网上查了一下,然后这是vim的一个强大功能。他可以录制一个宏(Macro),在开始记录后,会记录你所有的键盘输入,包括在insert模式下的输入、正常模式下使用的各种命令等。 具体使用: 第一步:在正常模式下(非insert模式、非visual模式)按下q键盘 第二步:选...

服务应用之WEB与WCF使用之见

APP_Code WEB: WebService.cs 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Services; 6 7 /// <summary> 8 /...

&amp;lt;Android基础&amp;gt;(一)

第一章Android 2003年10月,Andy Rubin等人创办了Android公司。2005年8月谷歌收购。 1.1 Android全貌 1.1.1 Android系统架构 1.Linux内核层:为Android设备的各种硬件提供底层驱动。如:显示驱动,音频驱动,蓝牙驱动,WiFi驱动,电源管理。 2.系统运行库层:c/c++库为Android层提供...