LINQPad over MVC4 WebApi - c#

I have prepared some code that builds a collection. I would like to use this collection in LINQPad for various queries.
As LINQPad allows WCF OData I thought that MVC4 WebApi would be perfect for this.
Unfortunatelly I wasn't able to make this work even with the template WebApi project.
When I try to add service to LINQPad I get "Data at the root level is invalid. Line 1, position 1.
I think the problem is that LINQPad doesn't use content negotiation and it is expecting xml but WebApi returns json.
Unfortunatelly Fiddler wasn't able to monitor the requests.
Anyone know how to fix this?

LinqPad does not have the REST drivers by default to query a Web API service. See: http://forum.linqpad.net/discussion/199/linqpad-error-when-access-odata-using-webapi
However, the following blog post by Filip W. speaks to this issue and how you can create a custom ControllerResolver that overrides the default resolver and allows running Web API from LinqPad. Have a look:
Hosting ASP.NET Web API in LinqPad
This post follows the same question: Using WebAPI in LINQPad?

why don't you just use the HttpClient class? HttpClient

Related

ASP.NET Web API with Linq2Couchbase dependancy injection

I'm new to ASP.NET Web API projects and also new to Couchbase. I'm trying to follow the instructions here: https://github.com/couchbaselabs/Linq2Couchbase/blob/master/docs/bucket-context.md
Using a blank project and the code provided, I get the error: "No parameterless constructor defined for this object". I know that I need to "inject" the BucketContext in some way, but I don't know where to put that, any ideas?
Perhaps following this tutorial will help you understand the Couchbase SDK in a bit more detail and let you understand the initialisation "challenge" you have.
http://blog.couchbase.com/2015/november/couchbase-dotnet-client-sdk-tutorial
In short, Couchbase Cluster is a "heavy" object and it's recommended to keep the object for the lifetime of the app. In WEB API that means that init should be done on app start. Depending on what version of ASP.NET you are using (ASP.NET 4.5 or ASP.NET vNEXT) init is done/recommended to be done different places.
ASP.NET 4.5 = global.asax
vNEXT = APP_START folder (look for other initialisations)
The above project/tutorial will explain step by step how to do the init.
When init is in place, linq2couchbase should work :)
Please let me know if this helped.

Making restful calls to asp.net web api

I am currently following this tutorial to create a simple REST service using Web Api. Note this is my first time doing something like this and I am just trying to learn more.
http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations
I have followed all the instructions and have it successfully running on my localhost. I understand that in this tutorial the URI for all my GET requests look something like:
localhostapi/products/id
And I understand that, and how to perform simple GET requests in the URI and see it actually occuring using my developer tools in my browser.
Now my question is... How do I make POST/DELETE/PUT requests and actually see what they are doing? The guide wasn't too clear, do I pass in parameters into the URI? Does the URI change when I want anything but a GET request? This text here seems to explain it but I do not understand:
The method takes a parameter of type Product. In Web API, parameters with complex types are deserialized from the request body. Therefore, we expect the client to send a serialized representation of a product object, in either XML or JSON format.
It's quite easy to make POST, PUT, DELETE requests. You just need to install Fiddler at http://www.telerik.com/download/fiddler
Next, install and run it. Go to the Composer tab on the right hand side. Next, put your local host URL, and the request method, and other data like the screenshot below
You can write unit tests, like
[TestMethod]
public void GetAllProducts_ShouldReturnAllProducts()
{
var testProducts = GetTestProducts();
var controller = new SimpleProductController(testProducts);
var result = controller.GetAllProducts() as List<Product>;
Assert.AreEqual(testProducts.Count, result.Count);
}
This link also This one may help.
more:
How to call ASP .NET MVC WebAPI 2 method properly
Sending C# object to webapi controller
You can set a breakpoint on your controller methods that handle the post/delete/put.
Same thing in your browser at the point where you call the post/delete/put (presumably in a jquery request)
You can also unit test your controller methods:
http://www.asp.net/mvc/tutorials/older-versions/unit-testing/creating-unit-tests-for-asp-net-mvc-applications-cs

Magento Go C# requesting API fails to initialize

I am trying to connect to Magento API using C#. I am using Magento Go service and from what I've read I am able to use their API - I hope I am not wrong here. So here is what I did:
I added a Service Reference to http://mydomain.gostorego.com/api/v2_soap?wsdl=1, and just adding a service worked fine. Now I created a test class with GetStuff() method, which looks like this:
using ww.Feeds.MagnetoGoService;
public static string GetStuff()
{
MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient s = new MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient();
var login = s.login("username here", "key here");
return login.ToString();
}
When I run the program I get an error in first line saying:
Could not find default endpoint element that references contract 'MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Any ideas what this may be? Do I have to set something up in my Magento Go settings? Or maybe using Magento Go is not allowing API access?
Thanks a lot.
Forget SOAP with c# you will pull your hair out. Download Charls Cook's xml-rpc api libary for c# and use the xml-rpc method. You won't get all the snazzy intellisense but at least it will work. There's also c# solution from ez.newsletter they released with Cook's library demonstrating how to use 80% of the magento api calls.
Cook's library xml-rpc.net
http://www.xml-rpc.net/
ez.newsletter solution
http://code.google.com/p/csharlibformagexmlrpcapi/
If anyone ever has problems with this, my solution was this:
I used the reference in one project, but I actually called the class and had main program in another project. You need your Service reference to be in each project wherever you're using it. That fixed it! Alternatively you can create a new BasicHttpBinding() and putt all the options from app.config/web.config into that binder, then you don't need to reference to Service everywhere. I hope that helps!

Running WCF WebAPI Prev 6 inside MVC3 on AppHarbor, 404 Errors

I was trying to throw a quick WCF WebAPI project together up on AppHarbor tonight and ran into some issues. The WCF API is couched inside an empty MVC3 project just like is demo'd on the WCF CodePlex site. https://github.com/jptoto/Postmark-Response-Code-Generator The API itself is quite basic, when you type an http response error code on the end of the url the response will be the proper error code. So, for example, using http://responsecodes.appharbor.com/api/response/405 in Fiddler or some other http client will return the proper error response for a 405 error. (This API is just a convenient way to test with proper error responses).
Anyway, no matter what I try I get 404 errors from AH. I can't tell if the routing isn't working on what. When I download the built code from AH and run it locally inside IIS it runs fine, no problem.
If AH just doesn't support some kind of routing that is in WebAPI Preview 6 that's fine. I just want to cover all my bases. Thanks!!
It seems that you are using the wrong URL and that your app is running here: http://responsecodes.apphb.com/api/response/406
(apphb instead of appharbor)

Restfull urls for ASP.NET page on IIS

Ok I had a huge Issue giving this a proper title, my excuses for that.
Anyways I have started slowly to look at Web and ASP.NET again, I am a C# developer but I have mostly worked with Windows applications the past 5 years or so, It is not that I haven't touched the web as such in that time, but this is as web services (Restfull as well as the ugly SOAP services) I have also worked with more "raw" web requests.
But I have not worked with IIS or ASP.NET in all that time.
What I would like to do is hos a web page that uses a URL style I could best describe with "like rest", hence the "Restfull urls" title. Because I think most people thinks of such URL's in terms of:
http://example.com/item/
http://example.com/item/23/
and so forth. Not that they have to look like that, however I would like to use such URL's instead of
http://example.com/item?id=23
I know subtext does this, but i have not had any luck finding it in their code base.
Now as far as I can tell I could just implement some IHttpHandler's, but at least for the examples I have seen of that, they write the page source back in code, and I still have master pages etc. I wish to use instead of taking over all that stuff my self, I really just kinda wants to route http://example.com/item/23/ to http://example.com/item and asking for the item with id 23...
I hope this makes sense at all >.<... And that someone has some better examples at hand that what I have been able to find.
You can achieve this using Routing here is a link to an MSDN blog, The .Net Endpoint - Using Routes to Compose WCF WebHttp Services that should get you started.
If you're looking at asp.net/IIS, another option to look at is ASP.Net MVC. It's pretty straight forward to create RESTful services.
Here's a tutorial:
http://www.codeproject.com/Articles/233572/Build-truly-RESTful-API-and-website-using-same-ASP
So here are your options-
For .net 3.5 sp1 framework with IIS7 you can use asp.net routing feature to have MVC style urls that you mentioned should create a custom route handler implementing IRouteHandler interface as explained here How to: Use Routing with Web Forms and register your route rules in Application_Start method in Global.asax. For your example you can register a route like this
routes.Add("ItemRoute", new Route
(
"item/{itemId}",
new CustomRouteHandler("~/item.aspx")
));
and then you can access itemId in your routed item.aspx page by checking request context item
requestContext.HttpContext.Items["itemId"]
For .net framework 4 MVC you dont have to create a custom handler, you can directly use
routes.MapPageRoute("ItemRoute", "item/{itemId}", "~/item.aspx");
in you global.asax Application_Start method.
This link explains more about the Routing
A way of achieve this is using URL rewriting.
If you're planning to host your Web application in Internet Information Services 7.x, you can take advantage of IIS URL Rewriting Module:
http://www.iis.net/download/urlrewrite
URL rewriting is just mapping a friendly URL to an unfriendly, common one, which is programming-friendly to inspect GET parameters.
For example:
http://yourdomain.com/item/48 => http://yourdomain.com/Items.aspx?Id=48

Categories

Resources