webapi

关于配置WebApi碰到的一个问题。

旧时模样 提交于 2019-12-04 03:45:17
十月新入职时候,接受一个项目,要用到webapi,当时公司的人发的项目不全,我写好api,却怎么也访问不到,甚是迷惑,于是翻阅资料, 发现在App_Start 文件夹中少了一个WebApiConfig.cs 文件 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 以及在Global.asax 文件中也要添加 一句 GlobalConfiguration.Configure(WebApiConfig.Register); 以前没有配置过webapi的东西, 废了一点功夫,,,,其实新建一个webapi的项目都是直接有这些配置的。 20191110记 来源: https://www.cnblogs.com/guhuaxia/p

net core WebApi——公用库April.Util公开及发布

元气小坏坏 提交于 2019-12-03 23:58:21
前言 在之前鼓捣过一次基础工程 April.WebApi 后,就考虑把常用的类库打包做成一个公共类库,这样既方便维护也方便后续做快速开发使用,仓库地址: April.Util_github , April.Util_gitee ,后续会继续推出基于Util的基础工程(包含权限相关)以及如果代码生成器觉得可以了也会推出,先mark不错过。 April.Util 首先,我们创建一个类库工程,当然新建之后就是删掉默认的类,我这里的版本是netstandard 2.0。 然后我们先缕下之前我做基础工程时候需要用到的通用型类库(当然后续会一直跟进补充)。 通用配置 AprilConfig,AprilEnums 缓存相关 CacheUtil,CookieUtil,SessionUtil,RedisUtil 日志相关 LogUtil 路径请求相关 UriUtil 回调相关 ResponseUtil 时间处理相关 DateUtil 验证码相关 CodeUtil 管理员相关 TokenUtil 加密相关 EncrypUtil Aop相关 AprilLogAttribute 其他扩展类 未完待续 我们所需要引入的NuGet包,尽可能的节省新工程需要引入的类库,这样统一类库环境,方便多工程简单部署问题,当然也可以只引入dll文件,自己在工程中引入需要的环境类库,引入类库列表。 log4net

C#调用WebApi

风流意气都作罢 提交于 2019-12-03 21:02:35
C#调用WebApi 1.WebRequest方式 Post: private void button1_Click(object sender, EventArgs e) { string ss= HttpPost("http://localhost:41558/api/Demo/PostXXX", "{Code:\"test089\",Name:\"test1\"}"); } public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "text/html, application/xhtml+xml, */*"; request.ContentType = "application/json";

WebAPI 使用控制台启动

旧街凉风 提交于 2019-12-03 17:11:15
using System; using System.Web.Http; using System.Web.Http.SelfHost; namespace UAC_OAuth2Center { public class Program { static void Main(string[] args) { try { var config = new HttpSelfHostConfiguration("http://localhost:2021"); // Web API 配置和服务 config.EnableCors(new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*", "*")); //config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional }); using (var sever = new HttpSelfHostServer(config)) { sever.OpenAsync().Wait(); Console

net core WebApi——使用xUnits来实现单元测试

帅比萌擦擦* 提交于 2019-12-03 16:50:56
目录 前言 单元测试 xUnit 小结 附录 前言 从开始敲代码到现在,不停地都是在喊着记得做测试,记得自测,测试人员打回来扣你money之类的,刚开始因为心疼钱(当然还是为了代码质量),就老老实实自己写完自己跑一遍,没有流程没有规划没有测试文档,就是自己整理一组数据跑一遍,最后依然还是让测试人员老老实实把一大堆测试问题扔给你。 单元测试 首先,还是来聊聊为啥要搞测试吧。 测试有助于代码整体健壮性,覆盖测试、压力测试都是为了全方位多角度更快更好为用户服务。 测试有助于提高程序猿的积极性以及引起自身的重视,毕竟一个坑栽一遍就够了,两次也能容忍,再三再四再五怕是要被搞,同时这也是自我提高的一种手段吧。 软件开发流程收尾的工作就是测试,绕不过,毕竟验收才是最终目标,达到效果才能获得应有的。 好了,聊完这些,当然我也不是专业测试人员,肯定不会给个测试文档模板,喏,照着这个规范起来,我主要是要鼓捣下我之前一直想试试的单元测试,这个自动化测试的手段之一,一直想试试但是一直都放着。 在 MSTest , NUnit , xUint 这三个中让我稍微犹豫了下,不过三七二十八管他呢,随便来个吧,就选了 xUnit ,当然MSTest是官方的,支持度应该高点儿,但是这不是我们该犹豫抉择的地方。 xUnit 首先,我们新建一个项目 April.Test 。 Fact 新建之后,我们看到有个默认的

WebApi路由机制详解

纵饮孤独 提交于 2019-12-03 13:19:18
从MVC到WebApi,路由机制一直都在其中扮演着重要的角色。 它可以很简单:如果你只需要会用一些简单的路由,如/Home/Index那么你只需要配置一个默认路由就能搞定。 它可以很神秘:你的url可以千变万化,看到一些“无厘头”的url,很难理解它是如何找到匹配的Action,例如/api/Pleasure/1/detail,这样的url可以让你纠结半天。 它可以很深奥:当面试官提问“请简单分析下MVC路由机制的原理”,你可能事先就准备好了答案,然后劈里啪啦一顿(型如:UrlRoutingMoudle—>Routes—>RouteData—>RequestContext—>Controller),你可能回答的很流利,但并不一定理解这些个对象到底是啥意思。):目前为止我还没能理解透,以后会继续努力的直到弄清楚。 一、MVC和WebApi路由机制比较 1、MVC使用的路由 在MVC中,默认路由机制是通过解析url路径来匹配Action。比如:/User/GetList,这个url就表示匹配User控制器下的GetList方法,这是MVC路由的默认解析方式。为什么默认的解析方式是这样子的呢?因为MVC定义了一个默认路由,路由代码放在App_Start文件夹下的RouteConfig.cs中,今后我们如果想要自定义路由规则,那自定义路由的代码也要写在RouteConfig.cs中。

Hosting WebAPI in Windows-Service crashes on first attempt

匿名 (未验证) 提交于 2019-12-03 10:10:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an console-app with hosted WebAPI (HttpSelfHostServer). In the console-app I have Controllers that query data from SQL-Server. The console-app works without problems (from fiddler and a Windows_client) Now I want to host the WebAPI (HttpSelfHostServer) in a Windows-Service. Therefore I have created a Service-Project and referenced the Controllers in console-app-dll so that the controllers should be found. To have a minimal chance to debug, I write some information in the event log from the windows service. I can start and stop the

WebAPI Mixing Implicit Flow and Client Credentials Flow

匿名 (未验证) 提交于 2019-12-03 09:52:54
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a WebAPI solution that secures it's controller methods via the [Authorize] attribute. It verifies that a given user has the appropriate roles, which are basically claims that come from an IdentityServer3. There are several single page application clients that interact with this WebAPI, and the client users are authenticated/authorized using implicit flow. So far pretty standard and simple, it all works fine... Now I have a need for background process to make calls to this same WebAPI. This effectively becomes machine to

Breeze WebAPI: How to combine QueryResult with ODataQueryOptions to return inlineCount

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I tried different ways of loading a list of items using breeze and the API Controller, using filters (partly using a custom object and another part using ODataQueryOptions), but none of them turn out to be really successful. Test code in javascript: function test() { EntityQuery .from("Products") /* .withParameters({ filters: [ { column: "Name", value: "12" } ]}) */ .orderBy("Name desc") .skip(30) .take(15) .inlineCount(true) .using(manager) .execute() .then(success) .fail(fail); function success(data) { console.log(data.products); console

WebAPI help page for complex type in URI

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using WebAPI help page to provide a documentation. Documentation is generated fine (from xml comments) except when I use complex model in the uri. So, I get the following doc GET service/Tst2 No documentation available. for the method: /// <summary> /// description /// </summary> [HttpGet] public string Tst([FromUri] TstModel filter) { return null; } public class TstModel { public int Id { get; set; } } for other methods with simple int/string parameters it works as expected. If I remove [FromUri] attribute it also works, but then So the