asp.net-web-api2

Return an error when invalid parameters are specified in ASP Web API

喜夏-厌秋 提交于 2019-12-02 08:43:38
I'm creating an API using C# and ASP.NET Web API and I want it to return an error when a parameter is used that isn't recognised. For example: /api/Events should a list of events /api/Events?startTime={{startTime}} should return a list of events that started at a particular time /api/Events?someRandomInvalidParameter={{something}} should return an error Is there a nice config way to do this? If not, how can I get a list of parameters to check myself. You could create an ActionFilter to automate this: public class InvalidQueryStringRejectorAttribute : ActionFilterAttribute { public override

Getting post array in Asp.net MVC

杀马特。学长 韩版系。学妹 提交于 2019-12-02 08:12:11
I am writing a web api2 program that handles sms-contacts. There is a JSON post to the server like this: { "Id":"4", "ClientId":"2", "NoOfRecipient":"3", "Numbers": { "num1":9898776568, "num2":9087674589 }, "Msg":"This is a test" } and in Server I have a class to handle this JSON as below: public class SmsData { public int Id { get; set; } public int ClientId { get; set; } public int NoOfRecipient { get; set; } public IEnumerable<Numbers> Numbers { get; set; } public string Msg { get; set; } public DateTime SentDate { get; set; } } public class Numbers{ public int Id { get; set; } public long

AngularJS Allow-Origin to WebApi2

◇◆丶佛笑我妖孽 提交于 2019-12-02 07:44:10
I want to host my API on a separate domain. I have configured my auth-interceptor for token in angular with a bearer: config.headers.Authorization = 'Bearer ' + sessionStorage.getItem('token'); In my My WebApi2 I have configured the WebApiConfig with cors. var cors = new EnableCorsAttribute("http://mydomain.com", "*", "*"); config.EnableCors(cors); And in web.config of the API I also included: <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> I still can´t access the api due to origin not allowed. Can I add something more in my header of the auth-interceptor

Why is it so hard to get route values outside controller context?

白昼怎懂夜的黑 提交于 2019-12-02 05:02:09
问题 I don't understand what's the deal behind this, why is it so easy to get route values inside Request of controller but nearly impossible to do the same thing on HttpContext.Current.Request ? Maybe I just don't know a better way and it exists. Can someone confirm that this is the only way to get route data outside controller? Example [Route("{id}"), HttpGet] public IHttpActionResult Test() { // Simple and easy var route1 = Request.GetRouteData().Values["id"]; // Wat. This is also ~6 times

Controller Versioning in Web Api 2 using URL

ε祈祈猫儿з 提交于 2019-12-02 04:38:26
I want to use URL-Based versioning for my controller. best solution that I found was code below. I'm looking for better solution for this. I tried Constrain It didn't work maybe i did some thing wrong my only concern is using controller with same name in different namespace...?! I userd string merging to creating wanted type. probably it is not a good idea. Please send good reference for this topic if you know any.....? public class ControllerVersioning : DefaultHttpControllerSelector { private HttpConfiguration _config; public ControllerVersioning(HttpConfiguration config) : base(config) {

How do I add properties to a Web API Response that I do not store in my DB?

白昼怎懂夜的黑 提交于 2019-12-02 03:53:31
I am building a C# Web API using Entity Framework 6.0. I have the simplest User Class with 3 properties that I persist on SQL into a User Table with 3 corresponding columns where UserID is its the Primary Key. public partial class User { public string UserID {get; set;} public string FirstName {get; set;} public string LastName {get; set;} } I want to add to the Web API two output-only properties on the fly that I do not care to store in my DB. I use these properties to communicate to the consuming client "Status" and "Message" information that are not part of the User Class. Status = OK|Error

How to download memory stream object via angularJS and webaAPI2

感情迁移 提交于 2019-12-02 03:53:20
I have a project where I am using NPOI to generate Excel document from my Angular application. I have the calls being made from my angular service to my webapi controller as follows: function exportReportToExcel(report) { return $http.post('reportlibrary/exportReport/', report, { }).then(function (response) { return response.data; }); }; Within the controller I make the following call [HttpPost] public HttpResponseMessage ExportReport([FromBody]DTOs.Report report) { try { IReportPersistenceManager manager = ContainerConfigurator.Instance.Resolve<IReportPersistenceManager>(); MemoryStream ms =

Autofac with Web API 2 - Parameter-less constructor error

折月煮酒 提交于 2019-12-02 03:43:17
问题 I am trying to get Autofac setup on my Web API 2 project. I have only used it a couple of times before. I used Nuget an installed both Autofac and Autofac.WebApi2 . Then in my Startup class I did this: public partial class Startup { public void Configuration(IAppBuilder app) { // Get our http configuration var config = new HttpConfiguration(); // Register the Autofac middleware FIRST. This also adds // Autofac-injected middleware registered with the container. var container =

ApiExplorer does not recognize route attributes with custom type

…衆ロ難τιáo~ 提交于 2019-12-02 03:42:31
I have a project where I want to use route attributes with a custom type. The following code where I have the custom type as a query parameter works fine and the help page displays the custom type. // GET api/values?5,6 [Route("api/values")] public string Get(IntegerListParameter ids) { return "value"; } WebApi.HelpPage gives the following documentation Help:Page If I change the code to use route attributes, the result is that I get an empty help page. // GET api/values/5,6 [Route("api/values/{ids}")] public string Get(IntegerListParameter ids) { return "value"; } When I inspect the code I

asp.net webapi 2 post parameter is always null

心已入冬 提交于 2019-12-02 03:05:14
I have been trying to figure this our for the last hour, but I can't see what's wrong with it the post is from a xamarin app that I'm writing, using restsharp portable client POST http://192.168.30.103:8080/api/Order HTTP/1.1 Authorization: Basic xxxx Content-Length: 51 Content-Type: application/json; charset=utf-8 Host: 192.168.30.103:8080 Connection: Keep-Alive Pragma: no-cache {"CheckinCount":1,"EventId":16630,"OrderNo":470292} It hits the server Post API correctly, but the parameter is always null public IHttpActionResult Post([FromBody]string source) { dynamic data = JsonConvert