asp.net-web-api2

Data annotations in Swagger

五迷三道 提交于 2019-11-29 09:15:41
I am using ASP.NET and Swagger that exposes a complex type that accepts a POST. It has a number of string fields that have different restricted lengths. How can I reflect that in the Swagger UI? You can annotate the properties with the StringLengthAttribute from System.ComponentModel.DataAnnotations . For instance: [StringLength(10)] public String Name {get;set;} will become: "name": { "minLength": 0, "maxLength": 10, "type": "string" } And this: [StringLength(10, MinimumLength = 5)] public String Name {get;set;} becomes: "name": { "minLength": 5, "maxLength": 10, "type": "string" } Besides

How to prevent a single object property from being converted to a DateTime when it is a string

风格不统一 提交于 2019-11-29 08:46:54
Here is a simplified version of the model I have to work with: class InputModel { public string Name { get; set; } public object Value { get; set; } } And the relevant parts of the controller: class Controller : ApiController { [HttpPut] public async Task<IHttpActionResult> Update([FromBody]InputModel model) { //implementation } } the Value property of the InputModel class could be of any type, and which type it will be is only known later on, in a piece of legacy code the model is sent to and that i have no control over. The problem I have occurs with the following json in the request body: {

Get Content-Disposition parameters

一世执手 提交于 2019-11-29 08:09:53
问题 How do I get Content-Disposition parameters I returned from WebAPI controller using WebClient? WebApi Controller [Route("api/mycontroller/GetFile/{fileId}")] public HttpResponseMessage GetFile(int fileId) { try { var file = GetSomeFile(fileId) HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(new MemoryStream(file)); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"

AngularJS With Asp.net Web API: $http post returning XMLHttpRequest cannot load: Response for preflight has invalid HTTP status code 405

≡放荡痞女 提交于 2019-11-29 07:52:34
问题 When trying to POST json to Asp.net web API server using $http it's returning the following error XMLHttpRequest cannot load http://localhost:62158/api/video/add. Response for preflight has invalid HTTP status code 405 but making the same request from $.ajax is working file. $HTTP Code $http.post(url, data, config) .success(function (data, status, headers, config) { defered.resolve(data); }) .error(function (data, status, header, config) { defered.reject(data); }); $.ajax Code $.ajax({ type:

Query parameter route constraints

瘦欲@ 提交于 2019-11-29 07:49:06
I just started using ASP.NET Web API 2.1 and have encountered a limitation. Using Attribute Routing, I can do the following: [Route("item/{id:int}")] public IHttpActionResult GetItem(int id) { ... } The URL /item/5 will be routed to this action, but the URL /item/abc will not, because of the int constraint in {id:int} . I tried changing my URL so that the id parameter was in the query string along with its constraint, despite the use of route constraints on query parameters never being mentioned or demonstrated in the documentation. [Route("item?{id:int}")] public IHttpActionResult GetItem(int

SwashBuckle/Swagger - OAuth Resource Owner Password Flow

♀尐吖头ヾ 提交于 2019-11-29 07:23:04
I'm trying to implement swagger into my Asp.Net Web API, and i'm running into a problem. I'm using the password resource owner flow, and i'm having to add a work around in order to do this, which is covered in the following stack overflow question :- Swagger/Swashbuckle: OAuth2 with Resource Owner Password Credentials Grant I've got everything working, the Bearer token is added via javascript to the request header in the current browser window, but the api calls to the controller methods requiring authorization are still return "401 - Authorization Failed". Here is the JavaScript that gets the

Unable to obtain configuration from well-known/openid-configuration

China☆狼群 提交于 2019-11-29 06:59:10
I am using ASP.NET 5, In my solution I have Web API, Identity Server and Angular 2 project and I am authenticating Angular 2 client by using Identity Server, Angular 2 client consumes web api by passing token in http request and web api authenticate token and gives response, for this I have written a custom attribute which checks that user is authenticated or not When I consume API I am getting following exception and Web API returns 500 internal server error. System.InvalidOperationException: IDX10803: Unable to obtain configuration from: ' http://xx.xx.xx.x:3926/.well-known/openid

Build JSON response in Web API controller

限于喜欢 提交于 2019-11-29 05:38:34
In a WebAPI project, i have a controller that checks a status of a product, based on a value the user enters. Lets say they enter "123" and the response should be "status": 1, AND a list of products. If they enter "321" the "status" is 0, AND a list of products. My question is, how do i build such a string correct in a WebAPI controller. [Route("{value:int}")] public string GetProducts(int value) { var json = ""; var products = db.Products; if (products.Any()) { foreach (var s in products) { ProductApi product = new ProductApi(); product.Name = s.Name; json += JsonConvert.SerializeObject

Web API self host - bind on all network interfaces

╄→尐↘猪︶ㄣ 提交于 2019-11-29 05:16:03
问题 How do you make a Web API self host bind on all network interfaces? I have the below code currently. Unfortunately, it binds only on localhost. So access to this server from other than localhost is failing. var baseAddress = string.Format("http://localhost:9000/"); using (WebApp.Start<Startup> (baseAddress)) { Console.WriteLine("Server started"); Thread.Sleep(1000000); } 回答1: Just change the base address like this var baseAddress = "http://*:9000/"; using (WebApp.Start<Startup> (baseAddress))

Include property but exclude one of that property's properties

两盒软妹~` 提交于 2019-11-29 04:17:28
Let's say I have a method like this in one of my controllers: [Route("api/Products")] public IQueryable<Product> GetProducts() { return db.Products .Include(p => p.Category); } Using this I can get a product from the database and include its Category property. In my CategoryControllerI have this method: [Route("api/Categories")] public IQueryable<Category> GetCategories() { return db.Categories .Include(c => c.Parent) .Include(c => c.Products) .Include(c => c.SubCategories); } When I send a GET request to the CategoryController this works as intended, I get the category, its parent, its