【c# – 网格计算API】教程文章相关的互联网学习教程文章

c# – 我应该返回状态代码还是在.Net Web Api 2中抛出异常【代码】

我见过像this这样的例子public IHttpActionResult GetProduct(int id) {Product item = repository.Get(id);if (item == null){throw new HttpResponseException(HttpStatusCode.NotFound);}return Ok(item); }但我也想象这是一个选择public IHttpActionResult GetProduct(int id) {Product item = repository.Get(id);if (item == null){return NotFound();}return Ok(item); }抛出异常或只返回NotFound(IHttpActionResult实例)是...

c# – 在输入API请求中找到未知属性时引发错误?【代码】

目前我正在将带有JSON字符串的Rest Request作为过滤器传递给Web API,如下所示,http://localhost:13825/api/values/get?filter={"Name":"abcdef","Address":"Something"}我有一个类如下,Public class Customer {public string Name{get;set;}public string Address{get;set;} }当我使用以下代码将JSON字符串解析为类对象时,它工作正常,public string Get(Customer filter) {}问题是,当我通过如下过滤器时,filter={"Name":"abcdef","...

c# – 如何将System.Net.ICredentials传递给API?【代码】

首先,使用所述API进行身份验证的凭据:Webservice.Authenticate.Credential credential = new Webservice.Authenticate.Credential {Username = "myUserName",Password = GetMD5("myPassword"), // See below for a MD5 encoding example methodApplicationId = new Guid("00000000-0000-0000-0000-000000000000"), //YOUR ID HEREIdentityId = new Guid("00000000-0000-0000-0000-000000000000") //Default is empty guid (no nee...

c# – 为什么我对Kairos api的REST请求说请求缺少必需的参数?【代码】

我正在尝试连接到kairos api以熟悉它并使用它们的检测功能. 这个api还没有正式的C#库.有一个似乎没有积极维护. 我正在尝试使用RestSharp直接连接.服务器正在接收我的请求,如果我省略app_id和app密钥进行身份验证,就会响应我的预期.所以那部分(添加标题)似乎正在起作用. 添加参数似乎失败了.根据他们的网站:https://www.kairos.com/docs/face-recognition/,唯一需要的参数是带有网址或base64编码照片的“图片”. 我添加了参数:va...

c# – 使用angular post将两个参数传递给WEB API调用【代码】

我的WEB API控制器中有以下post方法:public async Task<HttpResponseMessage> SendPost(Application application)我使用angular.js $http.post通过javascript调用它,并将应用程序参数作为JSON传递:$http.post("/api/AController/SendPost", JSON.stringify(application)).success(function (data, status, headers, config) { }这有效. 现在我想将第二个参数作为一个简单的字符串传递(我不能修改现有的应用程序JSON对象).我尝试了...

c# – XmlSerializer忽略WebApi中的[XmlAttribute]【代码】

我有一个返回一个简单对象的WebApi,但是当我强制它以XML格式返回(Accept:application / xml)时,它忽略了我在对象上设置的[XmlAttribute]属性. 这是我的目标:public class Foo {[XmlAttribute]public string Bar { get; set; } }我在代码中将其返回:[RoutePrefix("api/mytest")] public class MyTestController : System.Web.Http.ApiController {[HttpGet][Route("gettest")]public Foo GetTest(){return new Foo() { Bar = "fo...

c# – 具有多租户数据访问的Web API身份验证【代码】

我不确定我是否使用了正确的标题,但我无法想出更好的方式来描述它.这可能更像是一个设计问题. 我有一个多租户数据库,其中一个用户可以属于一个或多个实体.我通过调用/ token端点使用他/她的凭据对用户进行身份验证. 收到令牌后,我调用自己的终点(使用令牌)获取该用户的可用实体列表,然后允许该用户在内存缓存中设置其当前实体.然后,我在内存缓存中使用它来查找所有后续请求的实体/租户ID,以便在调用DB时知道用户“登录”了哪个实体...

c# – 我的桌面应用程序挂起了使用async和await的Web API服务调用【代码】

在数据库中保存数据时,我遇到了很多延迟.我有一个exe(Deskptop应用程序)从串口读取数据并通过Web API服务将该条目推送到数据库,但我的应用程序在此行上挂起:httpClient.PostAsync("api/MyController/Save", httpConent).Result;这个exe负责调用我的Web API服务方法并将数据保存到我的数据库. 这是我的代码:void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e){int dataLength = _serialPort.BytesToRe...

c# – 延迟API动作【代码】

我正在为我的软件编写API,它有很多接口,我的软件只是继承了它们.我希望API用户有可能在X毫秒后做一些事情,如下所示:public void PerformAction(Action action, int delay) {Task.Run(async delegate{await Task.Delay(delai);Form.BeginInvoke(action);// I invoke on the Form because I think its better that the action executes in my main thread, which is the same as my form's thread}); }现在我知道Task就像一个新线程...

c# – 有没有办法验证在web api控制器中创建的模型?【代码】

我有一个控制器,我的PUT方法使用multipart / form-data作为内容类型,所以我从而在控制器内获得JSON和映射类. 有没有办法可以在控制器内部模型类中编写的注释中验证此模型?public class AbcController : ApiController {public HttpResponseMessage Put(){var fileForm = HttpContext.Current.Request.Form;var fileKey = HttpContext.Current.Request.Form.Keys[0];MyModel model = new MyModel();string[] jsonformat = fileForm...

c# – 404传递web api参数时【代码】

我正在尝试使用此方法:public class DocumentenController : ApiController { [HttpPost][Route("DeleteDocument/{name}/{planId}")]public IHttpActionResult DeleteDocument(string name, int planId){_documentenProvider.DeleteDocument(planId, name);return Ok();} }这是WebApiConfig:config.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: "ActionApi",routeTemplate: UrlPrefix + "/{controller}/{actio...

c# – 是否可以使用在.net核心上运行的Identity Server 4和在.net 4.5.1上运行的WebApi应用程序?

请考虑以下情形.您有一个应用程序,用asp.net webapi实现api. 您正在设置一个新的Identity Server 4以用于您的基础架构.您需要api才能使用Identity Server. 如果是IdentityServer 3,则使用IdentityServer3.AccessTokenValidation. 如果您的api在.net核心上,您将使用IdentityServer4.AccessTokenValidation 但鉴于你的api使用旧式的asp.net webapi,甚至不是owin,而你的Identity Server基于.net核心,你有没有选择让它们一起工作?解决...

c# – 如何使用MoQ和NUnit在WebAPI 2中为ExceptionHandler编写单元测试【代码】

我有一个WebAPI,它使用自定义ExceptionHandler来处理所有异常.如何对此CustomExceptionHandler进行单元测试.任何领导都会有所帮助public class CustomExceptionHandler : ExceptionHandler {public override void Handle(ExceptionHandlerContext context){try{context.Result = new ResponseMessageResult(context.Request.CreateResponse(HttpStatusCode.InternalServerError, context.Exception));}catch (Exception){base.Hand...

c# – Microsoft Cognitive Service Vision API ClientException错误【代码】

我正在尝试使用Vision Cognitive Services来接收图像的描述,但我的代码总是抛出此异常:Exception Microsoft.ProjectOxford.Vision.ClientException HResult=0x80131500 Origine=<Non è possibile valutare l'origine dell'eccezione> Stack: in Microsoft.ProjectOxford.Vision.VisionServiceClient.HandleException (Exception exception) in Microsoft.ProjectOxford.Vision.VisionServiceClient.<SendAsync>b__42_1[TRequest,T...

c# – 使用API​​密钥进行Analytics Reporting v4【代码】

我需要在我使用asp.net Web表单(C#)构建的CMS Web应用程序的仪表板页面中集成来自Google Analytics的非常基本的报告. 我记得我能够在2015年使用API?? V3进行的测试中做到这一点,但是现在,使用V4,我总是收到一条错误消息,即验证需要OAuth2.I need to access to a specific analytics account I own, not the account of the user that navigate in the CMS!因此,我使用Google API Manager提供的API KEY.我已经给了API Key所有权限.A...