webapi

Include related entities with WebAPI OData request

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Is there any way to include related entities in an OData request? For example, I have a Person entity and a Task entity. The relationship is one-to-many, with a Person having many Tasks. If I query the data with the OData request: /odata/ Person to get all the Person entities, the json returned does not include a Tasks property for each Person. However, if I query the data with the OData request: /odata/ Person ( 14 )/ Tasks I get the collection of Tasks that belong to that Person. What I'm hoping to be able to do is get ALL of the

MVC WebApi HttpGet with complex object

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an existing WebApi action, that I want to switch from HttpPost to HttpGet. It currently takes a single complex object as parameter. The model: public class BarRequest { [JsonProperty("catid")] public int CategoryId { get; set; } } The controller: public class FooController : ApiController { //[HttpPost] [HttpGet] [ActionName("bar")] public void Bar([FromUri] BarRequest request) { if (request != null) { // CategoryId should be 123, not 0 Debug.WriteLine("Category ID :: {0}", request.CategoryId); } } } Now when I send the following

send multiple objects to webapi with angularjs

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have the following controller in my ASP.NET WebApi Application: [ Route ( "api/PutItem/" )] [ HttpPut ] public IHttpActionResult PutItem ( Guid id , Item item ) { if (! ModelState . IsValid ) } And I have the following in my "Items" AngularJs service var doEditItem = function ( item ){ var deferred = $q . defer (); console . log ( 'item' , item ); var config = { headers : { 'Authorization' : "Bearer " + $rootScope . token } //,params: {id:item.ItemId} } $http . put ( sitesettings . url () + "api/PutItem/" , item , config ) .

WebApi Multiple actions were found with GetAll() and GetByIds(int[] ids)

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Using the standard route: config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); With these actions: public class ValuesController : ApiController { // GET api/values public string GetAll() { return "all"; } // GET api/values/5 public string GetById(int id) { return "single"; } // GET api/values?ids=1&ids=2 public string GetByIds([FromUri] int[] ids) { return "multiple"; } And make a request to /api/values , I get this exception: Multiple actions were found

Encrypt Json result on WebApi Backend

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I finished my web api with data encriptions, but now, i have to encripty the Json result. When i put the querystring on the browser, now i have the answer (example) : I cannot show this...i have to show like (ENCRYPTED): [{"SUSPID":"AUAUAUA","IVNOME":"UAUAU","IVMAE":"UAUAU", ......] I am seeing some examples, but i am not finding one that is what i need Part of the code on my service (Cliente-side): var response = await client.GetAsync(urllink); var JsonResult = response.Content.ReadAsStringAsync().Result; if (typeof(T) == typeof(string))

Intercept webapi json formatting errors

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'd like to have a way to intercept the exception that occurs when you send in malformed json to a webapi endpoint, so that I can return a semantic error code as opposed to just 500. (e.g. "Fix your broken JSON or go to hell") 回答1: You can create your custom validation filter attribute by deriving from ActionFilterAttribute : public class ValidationFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { if (!actionContext.ModelState.IsValid) { actionContext.Response = actionContext

Asp.net WebApi的授权安全机制 Basic认证

青春壹個敷衍的年華 提交于 2019-12-03 07:03:47
1:Home/index.cshtml下面的Html代码 <div> <input value="1点击先登陆" type="button" id="btnLogin"/><br /> <input value="2再点击授权后调用接口" type="button" id="btnAuthenrazation" /><br /> </div>       2:Home/index 下面的HomeController [skipLogAttribute] public ActionResult Index() { return View(); } 3: Ajax的模拟代码,先登录,后获取授权,再带上Ticket,后台过滤器校验ok杂可以请求对应的接口 <script type="text/javascript"> $(function () { var ticket = ""; $("#btnLogin").click(function () { //---登陆的操作 $.ajax({ url: "http://localhost:8899/api/values", data: { "uid": "zrf", "pwd": "123" }, type: "get", success: function (res) { if (res.result) { ticket = res

Deadlock with ContinueWiths in WebAPI

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We've been running into a lot of deadlocks as part of exposing some of existing code over Web API. I've been able to distill the problem down to this very simple example that will hang forever: public class MyController : ApiController { public Task Get() { var context = TaskScheduler.FromCurrentSynchronizationContext(); return Task.FromResult(1) .ContinueWith(_ => { }, context) .ContinueWith(_ => Ok(DateTime.Now.ToLongTimeString()), context); } } To me, that code seems simple enough. This might seem a bit contrived but that's only because I

Wildcard in WebAPI Route template

匿名 (未验证) 提交于 2019-12-03 02:59:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have set up a route template: config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}/{*wildcard}", defaults: new { id = RouteParameter.Optional } ); controller's action signature: public IEnumerable<object> Get(Int64 id,string abc) I tried to match it with URL http://mymachine.com/api/Individuals/1?abc=4 , but it gives me an exception {"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.NullReferenceException",

.NET Core WebAPI + OpenIdDict (credentials flow) and Angular2 client: 401 after successful login (full repro)

匿名 (未验证) 提交于 2019-12-03 02:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm trying to create an Angular2 SPA consuming a .NET Core Web API protected using OpenIdDict, with credentials flow. In creating a repro solution for this issue, I have also detailed all my steps in a readme, so hope this post can be useful to newbies like me. Please find the full repro solutions in these repositories: server-side (.NET Core + OpenIdDict), with detailed instructions to build your own: https://github.com/Myrmex/repro-oidang client-side (Angular2): https://github.com/Myrmex/repro-angoid As for the server side, I