asp.net-web-api2

Passing multiple parameters to web API GET method

柔情痞子 提交于 2019-11-28 14:37:06
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 { MSN = m.zdjh, DateTime = m.sjsj, Signal_Strength = m.xhqd }).Distinct(); return Request.CreateResponse

Prompt file download

这一生的挚爱 提交于 2019-11-28 13:17:04
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] [Route("Generate/Report")] public IHttpActionResult GetMyReport(MyData myData) { byte[] myDoc =

Web Api: recommended way to return json string

醉酒当歌 提交于 2019-11-28 11:21:32
I've got a couple of services which already receive a json string (not an object) that must be returned to the client. Currently, I'm creating the HttpResponseMessage explicitly and setting its Content property to the json string which the service receives: var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonUtilizadores, Encoding.UTF8, "application/json"); return response; Now, is there a better way of doing this with the new IHttpActionResult ? Using the Content or Ok method ends up wrapping the json string with quotes, which is not what I want.

How to inject webapi AccountController in WebApi

我怕爱的太早我们不能终老 提交于 2019-11-28 10:51:36
问题 I have default constuctor and constructor with params like here: public class AccountController : ApiController { private const string LocalLoginProvider = "Local"; private ApplicationUserManager _userManager; public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; } [Dependency] public IRepository Repository{ get; private set; } public AccountController() { } public AccountController(ApplicationUserManager userManager, ISecureDataFormat<AuthenticationTicket>

Swashbuckle Swagger - How to annotate content types?

社会主义新天地 提交于 2019-11-28 09:50:25
How do I annotate my ASP.NET WebAPI actions so that the swagger metadata includes the content-types that my resources support? Specifically, I want the documentation to show that one of my resources can return the 'original' application/json and application/xml but also now returns a new format, application/vnd.blah+json or +xml . What you need to do is this; Swagger spec: you need to add your response-type to the list of response-types for that operation "produces": [ "application/json", "text/json" ], This can be done with an OperationFilter Pseudo Code incoming!!! public class

How to validate JWT Token in aspnet.core web api?

随声附和 提交于 2019-11-28 08:08:04
问题 I have created custom middleware class which validates the JWT token. I am calling this method before app.AddMvc() in configure method. *** I would like to know what are the things that I should add to Configuration services to authenticate my web API using JWT? I have added [Authorize] in my Controller class Do I need to call my middleware class which validates the JWT token first in Configure method? or I should call App.UseAuthentication() I am using the following order : app

WebAPI Return JSON array without root node

怎甘沉沦 提交于 2019-11-28 07:47:34
问题 I have the following sample code in an EmployeeController that creates a couple of employees, adds them to an employee list, and then returns the employee list on a get request. The returned JSON from the code includes Employees as a root node. I need to return a JSON array without the Employees property because whenever I try to parsethe JSON result to objects I get errors unless I manually reformat the string to not include it. public class Employee { public int EmployeeID { get; set; }

OPTIONS 405 (Method Not Allowed) web api 2

心不动则不痛 提交于 2019-11-28 07:25:30
I have created a web api 2 and I'm trying to do a cross domain request to it but I'm getting the following error: OPTIONS http://www.example.com/api/save 405 (Method Not Allowed) I have had a look around and most resolutions for this problem are saying that I need to install CORs from NuGet and enable it so I have installed the package and marked my controller with [EnableCors("*", "*", "*")] But this still hasn't resolved the problem. My ApiController only has the following Save method in: [ResponseType(typeof(int))] public IHttpActionResult Save(Student student) { if (ModelState.IsValid) {

How to pass Owin context to a Repo being injected into Api controller

落花浮王杯 提交于 2019-11-28 06:53:55
I've got a MVC WebApi owin (soft hosted) project, that uses Unity for resolving controller dependencies which look like this public class PacientaiController : ODataController { private readonly IEntityRepo<Ent.Pacientas> repo; public PacientaiController(IEntityRepo<Ent.Pacientas> repo) { this.repo = repo; } the problem I'm trying to solve - is how do I pass 'OwinContex' into a Repo. public class PacientasEntityRepo:IEntityRepo<Pacientas>,IDisposable { public PacientasEntityRepo(IOwinContext ctx) { ......... If I try to register it like this in the Startup.cs Container.RegisterType

WebApi 2 POST with single string parameter not working

混江龙づ霸主 提交于 2019-11-28 06:49:29
I have the following controller: public class ValuesController : ApiController { // POST api/values public IHttpActionResult Post(string filterName) { return new JsonResult<string>(filterName, new JsonSerializerSettings(), Encoding.UTF8, this); } } WebApi config config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); I use this js code to call the api $.ajax( { url: "/api/values/", type: "POST", dataType: 'json', data: { filterName: "Dirty Deeds" }, success: function (result) { console.log(result); }, error: