asp.net-web-api2

How to get base URL in Web API controller?

点点圈 提交于 2019-11-27 11:51:39
问题 I know that I can use Url.Link() to get URL of a specific route, but how can I get Web API base URL in Web API controller? 回答1: You could use VirtualPathRoot property from HttpRequestContext ( request.GetRequestContext().VirtualPathRoot ) 回答2: In the action method of the request to the url "http://localhost:85458/api/ctrl/" var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) ; this will get you http://localhost:85458 回答3: Url.Content("~/") worked for me! 回答4: This is what I use

CORS on OWIN and accessing /token causes 'Access-Control-Allow-Origin' error

回眸只為那壹抹淺笑 提交于 2019-11-27 10:54:41
问题 I am having trouble with securing my Web API using owin middle ware. I have installed below package Install-Package Microsoft.Owin.Cors -Version 2.1.0 And below is ConfigureAuth.cs code. public void ConfigureAuth(IAppBuilder app) { //... app.UseOAuthBearerTokens(OAuthOptions); ///Install-Package Microsoft.Owin.Cors -Version 2.1.0 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); } I have hosted this WebApi project on a link , say ,http://webaip.azurewebsites.net I am trying to access

Registering Web API 2 external logins from multiple API clients with OWIN Identity

北城余情 提交于 2019-11-27 10:01:22
I would like the following architecture (I've made up the product name for this example): Web API 2 application running on one server http://api.prettypictures.com MVC 5 client app running on another server http://www.webpics.com I would like www.webpics.com client app to use the Pretty Pictures API to: Register new accounts with username and password Register new accounts with Facebook/Google/Twitter/Microsoft Log in Retrieve pictures All of the above works except registering external accounts with Facebook, Google etc. I cannot work out the correct flow to create an external account from a

Swagger UI Web Api documentation Present enums as strings?

扶醉桌前 提交于 2019-11-27 09:39:13
问题 Is there a way to display all enums as their string value in swagger instead of their int value? I want to be able to submit POST actions and put enums according to their string value without having to look at the enum every time. I tried DescribeAllEnumsAsStrings but the server then receives strings instead of the enum value which is not what we're looking for. Has anyone solved this? Edit: public class Letter { [Required] public string Content {get; set;} [Required] [EnumDataType(typeof

ionic app cannot connect cors enabled server with $http

折月煮酒 提交于 2019-11-27 09:05:26
I am trying to build a mobile app with ionic framework. When my application tries to connect server to get json ( server is web api and cors is enabled) it just returns 404 on genymotion and real device. But when I run application in browser with ionic serve everything work fine. I am pretty sure CORS is functional. Here response header I got while application working in browser. Response Access-Control-Allow-Origin:* Cache-Control:no-cache Content-Length:395 Content-Type:application/json; charset=utf-8 Date:Fri, 08 May 2015 20:24:04 GMT Expires:-1 Pragma:no-cache Server:Microsoft-IIS/7.0 X

Newtonsoft JSON dynamic property name

最后都变了- 提交于 2019-11-27 08:53:01
Is there a way to change name of Data property during serialization, so I can reuse this class in my WEB Api. For an example, if i am returning paged list of users, Data property should be serialized as "users", if i'm returning list of items, should be called "items", etc. Is something like this possible: public class PagedData { [JsonProperty(PropertyName = "Set from constructor")]?? public IEnumerable<T> Data { get; private set; } public int Count { get; private set; } public int CurrentPage { get; private set; } public int Offset { get; private set; } public int RowsPerPage { get; private

Passing multiple parameters to web API GET method

狂风中的少年 提交于 2019-11-27 08:46:22
问题 I have created a WEB API using MySQL Database. The API works as expected for now. I sent a meter serial number and a date time parameter and then GET the expected result. Below is my controller public MDCEntities medEntitites = new MDCEntities(); public HttpResponseMessage GetByMsn(string msn, DateTime dt) { try { var before = dt.AddMinutes(-5); var after = dt.AddMinutes(5); var result = medEntitites.tj_xhqd .Where(m => m.zdjh == msn && m.sjsj >= before && m.sjsj <= after).Select(m => new {

Web API and OData- Pass Multiple Parameters

末鹿安然 提交于 2019-11-27 08:39:23
Is it possible to get OData to do the following? I would like to be able to query a REST call by passing on parameters than may not be the primary key. Can I call a REST method like --> GetReports(22, 2014) or Reports(22, 2014) ? [HttpGet] [ODataRoute("Reports(Id={Id}, Year={Year})")] public IHttpActionResult GetReports([FromODataUri]int Id, [FromODataUri]int Year) { return Ok(_reportsRepository.GetReports(Id, Year)); } Here is my latest change. //Unbound Action OData v3 var action = builder.Action("ListReports"); action.Parameter<int>("key"); action.Parameter<int>("year"); action

Post byte array to Web API server using HttpClient

こ雲淡風輕ζ 提交于 2019-11-27 08:36:01
I want to post this data to Web API server: public sealed class SomePostRequest { public int Id { get; set; } public byte[] Content { get; set; } } Using this code for server: [Route("Incoming")] [ValidateModel] public async Task<IHttpActionResult> PostIncomingData(SomePostRequest requestData) { // POST logic here } and this - for client: var client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:25001/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); var content = new

Prompt file download

爷,独闯天下 提交于 2019-11-27 07:35:36
问题 I have a link on my page on click of which I am trying to generate a PDF document and then show the "Open - Save" prompt on the browser. My HTML (reactjs component) has the below code where onclick calls the _getMyDocument function which then calls a Webapi method. <div className="row"> <a href="#" onClick={this._getMyDocument.bind(this)}>Test Link</a> </div> _getMyDocument(e) { GetMyDocument(this.props.mydata).then(()=> { }).catch(error=> { }); My Controller has the below code [HttpPost]