asp.net-web-api

Intercept webapi json formatting errors

非 Y 不嫁゛ 提交于 2020-01-01 18:50:06
问题 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

Do I need WCF if I can use ASP.net Web API

a 夏天 提交于 2020-01-01 18:21:27
问题 today I read a lot about ASP.net Web API. Do I understand it right, that if I need a WebService that only needs HTTP as transport, I can go for ASP.net API and don't need WCF? So far it looks as if Web API is much easier to use than WCF. Best Thomas 回答1: The answer to your question is basically yes. If you a) just want to use HTTP, b) want your content returned as XML or JSON, c) want to use a RESTful API and d) don't need any advanced messaging functionality, then web API is your best bet.

Do I need WCF if I can use ASP.net Web API

只谈情不闲聊 提交于 2020-01-01 18:21:09
问题 today I read a lot about ASP.net Web API. Do I understand it right, that if I need a WebService that only needs HTTP as transport, I can go for ASP.net API and don't need WCF? So far it looks as if Web API is much easier to use than WCF. Best Thomas 回答1: The answer to your question is basically yes. If you a) just want to use HTTP, b) want your content returned as XML or JSON, c) want to use a RESTful API and d) don't need any advanced messaging functionality, then web API is your best bet.

Listing API Methods Under Multiple Groups

扶醉桌前 提交于 2020-01-01 18:17:21
问题 I have Swashbuckle annotated code that looks like this: [Route("api/Subscribers/{id}/[controller]")] [Route("api/Organizations/{id}/[controller]")] public class AddressesController : Controller { [HttpGet("{aid}")] [SwaggerResponse(HttpStatusCode.OK, Type = typeof(PostalRecord))] public async Task<IActionResult> GetAddress(Guid id, Guid aid) { //do something } I would like to use the GroupActionsBy customization, as shown in this example, but I want to have the above GetAddress method

Kendo UI Datasource - Filter on releated data

空扰寡人 提交于 2020-01-01 17:02:28
问题 I am having trouble filtering a kendo data source on related data (many to many). I am using ASP.NET WebAPI2 and DataSourceRequest to catch the request on the server. The data is then fetched using the .ToDataSourceResult() extension method on an IQueryable. I am aware of this article http://blogs.telerik.com/kendoui/posts/12-11-29/the_facts_on_using_kendo_ui_with_asp_net_webapi My Data is structured as follows:- Customer -> CustomerAddress -> Address Where CustomerAddress is the join table

How to return error message from Catch block. Right now empty is returned

孤者浪人 提交于 2020-01-01 14:18:06
问题 My sample code of ApiKey validation is given below (I am using MVC4 web api RC): public class ApiKeyFilter : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext context) { //read api key from query string string querystring = context.Request.RequestUri.Query; string apikey = HttpUtility.ParseQueryString(querystring).Get("apikey"); //if no api key supplied, send out validation message if (string.IsNullOrWhiteSpace(apikey)) { var response = context.Request

How to perform async ModelState validation with FluentValidation in Web API?

筅森魡賤 提交于 2020-01-01 12:27:11
问题 I setup a web api project to use FluentValidation using the webapi integration package for FluentValidation. Then I created a validator that uses CustomAsync(...) to run queries against the database. The issue is that the validation seems to deadlock when awaiting for the database task. I did some investigation, it seems that the MVC ModelState API is synchronous, and it calls a synchronous Validate(...) method that makes FluentValidation to call task.Result , causing the deadlock. Is it

How to make ASP.NET WEB API HELP PAGES Xml Documentation Comments to have new line

Deadly 提交于 2020-01-01 11:57:08
问题 I have a rest api developed using ASP.NET WEB API. I used help pages nuget package in order to create documentation. One problem I encountered is the following. For my model objects, I have xml documentation comments and they become descriptions on the help pages for each member of the model. I want certain part of the description to be on a new line, but everything in the comment comes as one paragraph. I tried to add <br/> in the comments, but didn't help. Does anyone know how to achieve

Web API model validation and default values

元气小坏坏 提交于 2020-01-01 10:46:16
问题 This is the spiritual successor to my previous question Web API attribute routing and validation - possible?, which I think was too general to answer. Most of those issues are solved, but the default value question remains. Basically I have solved many pieces of the puzzle. I have this: [HttpGet] [Route("test/{id}"] public IHttpActionResult RunTest([FromUri]TestRequest request) { if (!ModelState.IsValid) return BadRequest(ModelState); return Ok(); } My TestRequest class: public class

Can Anyone Explain the work flow of IExceptionHandler with Sample Client Application

一个人想着一个人 提交于 2020-01-01 09:25:09
问题 I am facing below issues in this Sample: I am not able to find IsOutermostCatchBlock in ExceptionContext If Exception occurs, this HandleAsync method is executing twice. (http://www.asp.net/web-api/overview/web-api-routing-and-actions/web-api-global-error-handling) public class CustomExceptionHandler : IExceptionHandler { public virtual Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken) { if (!ShouldHandle(context)) { return Task.FromResult(0); } return