wcf-web-api

Validating model properties WCF Web APi

孤街醉人 提交于 2019-12-03 07:42:14
I have a set of services hosted with WCF Web Api, what I need to do is validate the properties inside the models of the app. In MVC 3 for example I decorate properties in the model like this: [StringLength(30)] public string UserName { get; set; } and then in the controller I proceed like this to verify os the model has met the validation parameters: [HttpPost] ActionResult Create(Model myModel) { if(ModelState.IsValid(){ Post the model } else { Don't post the model } } Is there a way to do something similar in WCF Web Api? Antony Scott Firstly I should say awesome question+answer Daniel

Ajax Post: 405 Method Not Allowed

↘锁芯ラ 提交于 2019-12-03 06:09:35
Within my API Controller called Payment, I have the following method: [HttpPost] public HttpResponseMessage Charge(Payment payment) { var processedPayment = _paymentProcessor.Charge(payment); var response = Request.CreateResponse(processedPayment.Status != "PAID" ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK, processedPayment); return response; } In my HTML page I have: $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "http://localhost:65396/api/payment/charge", data: $('#addPayment').serialize(), dataType: "json", success: function (data) { alert(data); } }

Programmatically set InstanceContextMode

筅森魡賤 提交于 2019-12-03 04:13:01
Is there a way to do this ... [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] ...programmatically? The reason is that I want to pass in an instance of my service directly into my self-hosting helper class when integration testing my service. I'm using Castle Windsor to create all my objects, which works fine when using the test web site. But I get the following error when I try to use my HttpWebService helper class ... System.InvalidOperationException was unhandled by user code Message=In order to use one of the ServiceHost constructors that takes a service instance, the

Autofac and IDisposable interface

六月ゝ 毕业季﹏ 提交于 2019-12-03 02:05:16
Assuming that I have the following interface and class: public interface IFooRepo : IDisposable { //... } public FooRepo : IFooRepo { //Methods here //Properly implement the IDisposbale.Dispose() here } I use Autofac as IoC container in my application and if I register this as below, can I be sure that it will disposed properly? private static IContainer RegisterServices(ContainerBuilder builder) { builder.RegisterType<FooService>().As<IFooService>(); return builder.Build(); } Or should I take further steps depending on the application type I am using. (In this case, I using ASP.NET MVC but I

DELETE method .NET WebAPI does not work

落爺英雄遲暮 提交于 2019-12-01 11:45:43
I've seen tons of posts about this, but the DELETE method of my new WebAPI simply does not work and returns a 404, using Windows 7 32-bit, IIS 7.5. I've tried Uninstalling WebDAV Adding PUT, DELETE, OPTIONS to the ExtensionlessUrlHandler-Integrated-4.0 handler (and 32bit/64bit handlers). Allowing all modules to run. All to no avail and all return 404. If I change the DELETE type to a GET, then the service runs the GET command perfectly fine. Anyone with any other ideas about this? It's driving me crazy. EDIT: I'm calling the DELETE method like this (mediator is a wrapper around the jQuery call

WebApi HttpClient for .NET framework 3.5 [closed]

点点圈 提交于 2019-12-01 04:12:57
Is there any package available of WebApi for .Net framework 3.5? I'm specifically looking forward to use HttpClient component of WebApi. Is there any alternatives that work on .Net 3.5? A very mature and very robust library that you can use for HTTP Requests is RestSharp . It has support for OAuth, pluggable serialization, and other HTTP concerns. It supports NET 3.5+, Silverlight 4, Windows Phone 7, Mono, MonoTouch. It's open source and available via github. Here is a blog post about consuming WebAPI services with RestSharp. Check it out You can use http://www.servicestack.net/ client for

Removing headers from the response

心不动则不痛 提交于 2019-12-01 02:44:42
I need to cloak certain headers generated by ASP.NET and IIS and returned in the responses from a ASP.NET WebAPI service. The headers I need to cloak are: Server X-AspNet-Version X-AspNetMvc-Version X-Powered-By The service was earlier hosted in WCF, and the cloaking was done in an HttpModule by subscribing to PreSendRequestHeaders and manipulating HttpContext.Current.Response.Headers. With ASP.NET WebAPI everything is now task based, so HttpContext.Current is null. I tried to insert a message handler and manipulate the returned HttpResponseMessage, but the headers were not present on that

Can I have both a Controller and an ApiController for the same thing?

社会主义新天地 提交于 2019-12-01 02:30:23
I've just started to use the VS 2012 RC, and I'm creating an ASP.NET MVC 4 web application in which I plan to provide both an HTML-based user interface and a WebApi-based programming interface. For my HTML website, I have a controller and view for each of my models (MVC!), and the routing works "by convention" so that, for example, the URL /client hooks up to my ClientController . My ClientController derives from Controller . For my API, I will create new controllers that derive from ApiController . I naturally want my API URLs to be similar to my HTML URLs, so I'd like the client info to be

Removing headers from the response

寵の児 提交于 2019-11-30 22:31:19
问题 I need to cloak certain headers generated by ASP.NET and IIS and returned in the responses from a ASP.NET WebAPI service. The headers I need to cloak are: Server X-AspNet-Version X-AspNetMvc-Version X-Powered-By The service was earlier hosted in WCF, and the cloaking was done in an HttpModule by subscribing to PreSendRequestHeaders and manipulating HttpContext.Current.Response.Headers. With ASP.NET WebAPI everything is now task based, so HttpContext.Current is null. I tried to insert a

Can I have both a Controller and an ApiController for the same thing?

眉间皱痕 提交于 2019-11-30 22:07:42
问题 I've just started to use the VS 2012 RC, and I'm creating an ASP.NET MVC 4 web application in which I plan to provide both an HTML-based user interface and a WebApi-based programming interface. For my HTML website, I have a controller and view for each of my models (MVC!), and the routing works "by convention" so that, for example, the URL /client hooks up to my ClientController . My ClientController derives from Controller . For my API, I will create new controllers that derive from