C# MVC & WebAPI 2 示例应用类别管理实现

需积分: 9 0 下载量 38 浏览量 更新于2024-11-06 收藏 25.6MB ZIP 举报
资源摘要信息:"示例应用sampleapp_csharp_mvc_webapi是一个构建在*** MVC 5、C#语言、Web API 2、AngularJS框架之上的应用程序,主要面向使用Mono运行环境的开发者。本资源提供了如何在一个实际项目中将这些技术整合在一起的示例,这对于那些希望了解如何在现代Web开发中应用这些技术的开发者们来说是一份宝贵的参考。" *** *** MVC(Model-View-Controller)是一种构建Web应用程序的框架,它提供了一种将业务逻辑、数据模型和用户界面分离的方式。MVC 5是*** MVC框架的第五个主要版本,提供了许多新特性和改进,例如单点登录、OAuth和OpenID支持、Bootstrap的支持、自定义路由配置以及异步操作等。这些功能极大地提升了Web应用的构建效率和用户体验。 C# C#是一种面向对象的编程语言,由微软开发,广泛用于开发Windows平台上的应用程序。C#具备类型安全、内存管理以及许多高级功能,如泛型、委托、LINQ等。在本示例应用中,C#被用于编写服务器端的逻辑,包括处理HTTP请求和响应、数据库操作以及业务逻辑处理等。 Web API 2 Web API 2是一个用于构建RESTful服务的框架,允许开发者构建Web服务,该服务可以通过HTTP发送和接收数据。Web API 2兼容性良好,可以被任何平台上的客户端所调用,它提供了许多支持跨平台和跨设备通信的功能,如支持路由配置、数据格式化、媒体类型支持等。这对于创建与其他平台和设备兼容的应用程序非常关键。 AngularJS AngularJS是一个开源的JavaScript框架,由Google维护,它通过使用HTML作为模板语言,可以极大地扩展HTML的语法。AngularJS的主要特性包括依赖注入、双向数据绑定、路由、模板和指令等,这些特性使得构建动态单页应用程序(SPA)变得更加容易。AngularJS可以与*** MVC应用程序很好地集成,提供强大的前端解决方案。 Mono Mono是一个开源的.NET框架实现,可以在非Windows平台上运行.NET应用程序,包括Linux、Mac OS X和Unix。它支持C#、***和CLI语言,并允许开发者使用.NET技术在多种操作系统上构建和运行应用程序。本示例应用中提到的mono标签表明这个示例应用可以被运行在支持mono的环境中。 文件名称列表中的"sampleapp_csharp_mvc_webapi-master"暗示了这是一个包含源代码的仓库,通常master分支包含了应用程序的主要代码版本。开发者可以通过获取这个压缩包子文件来下载和研究应用的源代码,从而理解如何将上述技术应用于实际项目中。 整体来看,sampleapp_csharp_mvc_webapi是一个综合性的示例应用,涵盖了现代Web应用程序开发中的多种技术和实践。通过对它的学习,开发者可以获得如何在企业级Web应用程序开发中整合最新技术的宝贵经验。此外,它还特别关注了如何在非Windows平台上部署和运行.NET应用程序,这是对于希望在多样化环境中部署.NET应用的开发者特别有帮助的部分。
2018-08-23 上传
目录 C#进阶系列——WebApi 路由机制剖析:你准备好了吗? ................................................................................................... 3 一、MVC和WebApi路由机制比较 ........................................................................................................................................ 5 1、MVC里面的路由 ......................................................................................................................................................... 5 2、WebApi里面的路由 ................................................................................................................................................... 5 二、WebApi路由基础 ............................................................................................................................................................. 7 1、默认路由 ..................................................................................................................................................................... 7 2、自定义路由 ............................................................................................................................................................... 10 2.1、自定义路由一:匹配到action ................................................................................................................. 11 2.2、自定义路由二 ............................................................................................................................................... 12 3、路由原理 ................................................................................................................................................................... 12 三、WebApi路由过程 ........................................................................................................................................................... 13 1、根据请求的url匹配路由模板 .............................................................................................................................. 13 2、找到控制器 ............................................................................................................................................................... 13 3、找到action ............................................................................................................................................................. 15 四、WebApi特性路由 ........................................................................................................................................................... 16 1、启动特性路由 ........................................................................................................................................................... 17 2、最简单的特性路由 ................................................................................................................................................... 17 3、带参数的特性路由 ................................................................................................................................................... 19 4、参数的约束和默认值 ............................................................................................................................................... 20 5、路由前缀 ................................................................................................................................................................... 20 五、第一个Restful风格的WebApi服务 ......................................................................................................................... 21 六、总结 ................................................................................................................................................................................. 22 C#进阶系列——WebApi 异常处理解决方案 ......................................................................................................................... 23 一、使用异常筛选器捕获所有异常 ..................................................................................................................................... 24 1、接口级别 ........................................................................................................................................................... 31 2、控制器级别 ....................................................................................................................................................... 33 3、全局配置 ........................................................................................................................................................... 34 二、HttpResponseException自定义异常信息 .............................................................................................................. 35 三、返回HttpError ............................................................................................................................................................ 37 四、总结 ................................................................................................................................................................................. 38 C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解 ......................................................................................... 39 一、void无返回值 ............................................................................................................................................................... 40 二、IHttpActionResult .................................................................................................................................................... 42 1、Json(T content) ........................................................................................................................................... 42 2、Ok()、 Ok(T content) ................................................................................................................................. 45 3、NotFound() ............................................................................................................................................................. 47 4、其他 ........................................................................................................................................................................... 49 4.1、Content(HttpStatusCode statusCode, T value) .................................................................. 49 4.2、BadRequest() ............................................................................................................................................ 49 4.3、Redirect(string location) ................................................................................................................ 50 5、自定义IHttpActionResult接口的实现 ............................................................................................................ 50 三、HttpResponseMessage ............................................................................................................................................... 53 四、自定义类型 ..................................................................................................................................................................... 54 五、总结 ................................................................................................................................................................................. 55 C#进阶系列——WebApi 接口参数不再困惑:传参详解 ..................................................................................................... 56 一、get请求 ......................................................................................................................................................................... 57 1、基础类型参数 ........................................................................................................................................................... 57 2、实体作为参数 ........................................................................................................................................................... 58 3、数组作为参数 ........................................................................................................................................................... 62 4、“怪异”的get请求 .................................................................................................................................................. 62 (1)WebApi的方法名称以get开头 ................................................................................................................ 62 (2)WebApi的方法名称不以get开头 ............................................................................................................ 64 二、post请求 ....................................................................................................................................................................... 66 1、基础类型参数 ........................................................................................................................................................... 66 (1)错误的写法 ................................................................................................................................................... 66 (2)正确的用法 ................................................................................................................................................... 67 (1)错误写法 ....................................................................................................................................................... 68 (2)正确用法 ....................................................................................................................................................... 69 (3)推荐用法 ....................................................................................................................................................... 70 2、实体作为参数 ........................................................................................................................................................... 70 (1)单个实体作为参数 ....................................................................................................................................... 70 (2)实体和基础类型一起作为参数传递 ........................................................................................................... 73 3、数组作为参数 ........................................................................................................................................................... 74 (1)基础类型数组 ............................................................................................................................................... 74 (2)实体集合 ....................................................................................................................................................... 74 4、后台发送请求参数的传递 ....................................................................................................................................... 75 三、put请求 ......................................................................................................................................................................... 77 1、基础类型参数 ........................................................................................................................................................... 77 2、实体作为参数 ........................................................................................................................................................... 77 3、数组作为参数 ........................................................................................................................................................... 78 四、delete请求 ................................................................................................................................................................... 78 五、总结 ................................................................................................................................................................................. 79 C#进阶系列——WebApi 身份认证解决方案:Basic基础认证.......................................................................................... 80 一、为什么需要身份认证 ..................................................................................................................................................... 81 1、我们不加身份认证,匿名用户可以直接通过url随意访问接口: ........................................................... 81 2、增加了身份认证之后,只有带了我们访问票据的请求才能访问我们的接口。 ....................................... 82 二、Basic基础认证的原理解析 ......................................................................................................................................... 84 1、常见的认证方式 ....................................................................................................................................................... 84 2、Basic基础认证原理 ............................................................................................................................................... 85 三、Basic基础认证的代码示例 ......................................................................................................................................... 85 1、登录过程 ................................................................................................................................................................... 85 1.1、Web前端 ....................................................................................................................................................... 85 1.2、登录的API接口 .......................................................................................................................................... 86 2、/Home/Index主界面 .............................................................................................................................................. 88 3、WebApiCORS验证部分(重点) ............................................................................................................................ 90 3.1、在WebApiCORS项目里面自定义一个类RequestAuthorizeAttribute,去继承我们的AuthorizeAttribute这个类。然后重写OnAuthorization方法,在这个方法里面取到请求头的Ticket信息,然后校验用户名密码是否合理。 ............................................................................................................. 90 3.2、在具体的Api接口增加我们上面自定义类的特性 .................................................................................. 91 四、优化 ................................................................................................................................................................................. 92 1、解决API的问题 ...................................................................................................................................................... 92 2、解决ajax的问题 .................................................................................................................................................... 93 3、解决特殊不想使用验证的方法 ............................................................................................................................... 94 五、总结 ................................................................................................................................................................................. 95 C#进阶系列——WebApi 跨域问题解决方案:CORS ............................................................................................................. 95 一、跨域问题的由来 ............................................................................................................................................................. 96 二、跨域问题解决原理 ......................................................................................................................................................... 96 三、跨域问题解决细节 ......................................................................................................................................................... 96 1、场景描述 ................................................................................................................................................................... 97 2、场景测试 ................................................................................................................................................................... 99 1)我们不做任何的处理,直接将两个项目运行起来。看效果如何 ............................................................... 99 2)使用CORS跨域 ................................................................................................................................................ 99 3)CORS的具体参数设置。 ............................................................................................................................... 103 四、总结 ............................................................................................................................................................................... 104 C#进阶系列——WebApi 接口测试工具:WebApiTestClient ........................................................................................ 105 一、WebApiTestClient介绍 ........................................................................................................................................... 106 二、WebApiTestClient展示 ........................................................................................................................................... 106 三、WebApiTestClient使用 ........................................................................................................................................... 116 1、如何引入组件 ......................................................................................................................................................... 116 2、如何使用组件 ......................................................................................................................................................... 118 1、修改Api.cshtml文件 ................................................................................................................................. 118 2、配置读取注释的xml路径 ............................................................................................................................ 119 3、测试接口 ......................................................................................................................................................... 121 四、总结 ............................................................................................................................................................................... 123

在划线处完成SampleApp工程应用层初始化函数代码的注释(用中文简述各段代码)。 void SampleApp_Init( uint8 task_id ) { SampleApp_TaskID = task_id; SampleApp_NwkState = DEV_INIT; SampleApp_TransID = 0; // #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SampleAppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES // #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to start the device. ZDOInitDevice(0); #endif // SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast; SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF; // SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP; // SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; SampleApp_epDesc.task_id = &SampleApp_TaskID; SampleApp_epDesc.simpleDesc=(SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc; SampleApp_epDesc.latencyReq = noLatencyReqs; // Register the endpoint description with the AF afRegister( &SampleApp_epDesc ); // Register for all key events - This app will handle all key events RegisterForKeys( SampleApp_TaskID ); // By default, all devices start out in Group 1 SampleApp_Group.ID = 0x0001; osal_memcpy( SampleApp_Group.name, "Group 1", 7 ); aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group ); }

2023-06-05 上传