Web APIhttp://forums.asp.net/1246.aspx/1?Web+APIDiscussions on building and consuming HTTP services using ASP.NET Web API.Sun, 19 May 2013 03:59:59 -0400urn:uuid:00000000-0000-0000-0000-000000001246urn:uuid:00000000-0000-0000-0000-000005399837http://forums.asp.net/p/1908235/5399837.aspx/1?Self+hosting+or+IIS+hosted+Self hosting or IIS hosted? <p>I've found very little in regards to the difference's (pros / cons) of going the Self-hosted route or the standard IIS hosted with a&nbsp;Web Api service.&nbsp; I'm sure others out there feel the same and was hoping a few local experts could share their thoughts on it to get a good reference on which way to go out here for people to view.</p> <p>In my case we're going the service route to expand out our reach for mobile type devices, but it still leaves me questioning if there is a right or wrong way to go with this or basically in what scenario would one opt for a &quot;Self-Hosted&quot; service over IIS?</p> <p><span style="text-decoration:underline"><strong>What I've found (basically just pros for IIS hosted):</strong></span></p> <p><em>- You lose all of the features of IIS (logging, application pool scaling, throttling/config of your site, etc.)...</em></p> <p><em>&nbsp;- You have to build every single feature that you want yourself HttpContext?</em></p> <p><em>&nbsp;- You lose that since ASP.NET provides that for you. So, I could see that making things like authentication much harder WebDeploy? </em></p> <p><em>- IIS has some nice specific features in 8 about handling requests and warming up the service (self-hosted does not) </em></p> <p><em>- IIS has the ability to run multiple concurrent sites with applications and virtual directories to advanced topics like load balancing and remote deployments.</em></p> 2013-05-20T13:51:56-04:002013-05-20T13:51:56.827-04:00urn:uuid:00000000-0000-0000-0000-000005400951http://forums.asp.net/p/1908481/5400951.aspx/1?OData+EntitySetConfiguration+and+Reflection+doesn+t+workOData EntitySetConfiguration and Reflection doesn't work <p>Hi all,</p> <p>I want to map some properties of my Entity for OData conroller. I need to use Reflection to determine which properties will be mapped. This is my code:</p> <pre class="prettyprint">private static void ConfigureEntities(ODataModelBuilder modelBuilder, string typeName) { var entitySetConfiguration = EntityTypeConfiguration&lt;Entity&gt;(modelBuilder, typeName); var entityType = entitySetConfiguration.EntityType; entityType.HasKey(x =&gt; x.EntityId); foreach (PropertyInfo property in typeof(Entity).GetProperties()) { if (property.PropertyType.FullName == &quot;System.Boolean&quot;) { var property1 = property; entityType.Property(arg =&gt; property1); } } }</pre> <p>This code causes the following error:</p> <p><em>The type 'System.Reflection.PropertyInfo' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method System.Web.Http.OData.Builder.StructuralTypeConfiguration&lt;TStructuralType&gt;.Property&lt;T&gt;(System.Linq.Expressions.Expression&lt;System.Func&lt;TStructuralType,T&gt;&gt;)'</em></p> <p>I tried this with several versions of ODataLib: 5.2.0, 5.4.0, 5.5.0-rc1</p> 2013-05-21T11:54:51-04:002013-05-21T11:54:51.247-04:00urn:uuid:00000000-0000-0000-0000-000005400898http://forums.asp.net/p/1908473/5400898.aspx/1?WEB+API+Windowsauthentication+enabled+Sencha+not+authorized+401+WEB API - Windowsauthentication enabled - Sencha not authorized [401] <p>Hi all,</p> <p>i got a basic ASP.NET MVC WEB API with windows authentication enabled and anonymous authentication disbabled.</p> <p>To process with the JSON data i use Sencha. If i try to load the store of it, it tells me that Sencha is not authorized [401].</p> <p>Is there a way to keep the windowsauthentication as &quot;enabled&quot; and allow Sencha to send request with ajax?<br> <br> Thanks alot<br> Livermorium&nbsp;</p> 2013-05-21T11:11:23-04:002013-05-21T11:11:23.793-04:00urn:uuid:00000000-0000-0000-0000-000005400325http://forums.asp.net/p/1908347/5400325.aspx/1?Serializing+Entity+with+Foreign+Key+to+itself+ERRORSerializing Entity with Foreign Key to itself, ERROR <p>Hi all,</p> <p>I have a table that has foreign key to itself. this entity is used for hierarchycal data. When I create a WebApi, to generate JSon, I'm having the following message:</p> <pre class="prettyprint">&lt;ExceptionMessage&gt; Object graph for type 'System.Collections.Generic.HashSet`1[[Admin.Model.ChartOfAccount, Admin.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled. &lt;/ExceptionMessage&gt;</pre> <p>Any help are very helpful</p> 2013-05-21T02:19:02-04:002013-05-21T02:19:02.673-04:00urn:uuid:00000000-0000-0000-0000-000005400347http://forums.asp.net/p/1908356/5400347.aspx/1?File+could+not+be+uploadedFile could not be uploaded <p>i used follwing code to attached file to api asyc post method but unfortunatly this error occuuerd</p> <pre class="prettyprint">using (var client = new HttpClient()) using (var content = new MultipartFormDataContent()) { client.BaseAddress = new Uri(&quot;http://localhost:3635/&quot;); var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(@&quot;C:\Project\New folder\FileHub\SourceCode_v2.5\SpecFlow\UploadDocument\Hydrangeas.jpg&quot;)); fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue(&quot;attachment&quot;) { FileName = &quot;Hydrangeas.jpg&quot; }; content.Add(fileContent); var result = client.PostAsync(&quot;http://localhost:3635/api/File&quot;, content).Result; Console.WriteLine(result.StatusCode); }</pre> <p><br> File could not be uploaded</p> 2013-05-21T03:32:43-04:002013-05-21T03:32:43.213-04:00urn:uuid:00000000-0000-0000-0000-000005400333http://forums.asp.net/p/1908350/5400333.aspx/1?Travel+APITravel API <p>I want to develope a site which work like hotwire.com or orbitz.com to find cheap deals on car, hotels, ticket etc. I know Expedia offer EAN but their pricing is prity high than all other site like hotwire.com. Does anyone know any API that will allow me to search and book from asp.net application. I wonder if anyone know how hotwire.com or orbitz.com works and what api they use to find the deals. Any programing script help will be really helpful</p> 2013-05-21T02:51:37-04:002013-05-21T02:51:37.193-04:00urn:uuid:00000000-0000-0000-0000-000005400262http://forums.asp.net/p/1908332/5400262.aspx/1?Invalid+Control+Characters+in+ContentType+REST+clientInvalid Control Characters in ContentType - REST client <p>Getting the error:</p> <p>Specified value has invalid Control characters.<br> Parameter name: value</p> <p>But this is what the API uses for it's content type?</p> <pre class="prettyprint">wRequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest) wRequest.Method = &quot;POST&quot; wRequest.Headers.Add(&quot;Authorization&quot;, &quot;Basic &quot; &#43; AuthInfo) wRequest.PreAuthenticate = True wRequest.ContentType = &quot;application/vnd.net.juniper.space.device-management.discover-devices&#43;xml;version=1;charset=UTF-8&quot;</pre> <p>Any ideas how to get around this?<br> &nbsp;</p> 2013-05-20T21:43:07-04:002013-05-20T21:43:07.53-04:00urn:uuid:00000000-0000-0000-0000-000005398897http://forums.asp.net/p/1908021/5398897.aspx/1?About+using+Web+API+controllers+from+external+dllAbout using Web API controllers from external dll <p>Hi folks!</p> <p>Can any one explain me how to use ApiController's from other dll? I hearing, when route accept request on httpRoute, it search all referenced dll where <span id="result_box" class="short_text" lang="en"><span class="hps">values</span> <span class="hps">coincide</span> <span class="hps">with exposed</span></span>.</p> <p>Thank's for answer</p> 2013-05-19T09:59:08-04:002013-05-19T09:59:08.207-04:00urn:uuid:00000000-0000-0000-0000-000005399638http://forums.asp.net/p/1908202/5399638.aspx/1?Problem+with+%C3%86%C3%98%C3%85+characters+when+using+http+headers+Web+API+Problem with ÆØÅ characters when using http headers (Web API) <p>In my ASP.Net Web Api I have a Get function where input info is sent in the http header. It works fine, except when the http header contains Norwegian characters like Æ, Ø and Å. This characters is transformed to æ ø Ã¥. When the same characters (Æ,Ø,Å) are sent as parameters in the URL it works fine. When testing the script on my local PC it works fine to send Æ, Ø and Å in the http header as well.<br> I don't know why norwegian characters can't be sent in the http header, but I guess this is an encoding / charset problem. After hours of testing I still can't find any way fo fix it.</p> <p><br> <strong>Web.config:</strong></p> <p><code id="questionViewInlineCode10-28133093-1">&lt;globalization fileEncoding=&quot;utf-8&quot; requestEncoding=&quot;utf-8&quot; responseEncoding=&quot;utf-8&quot; culture=&quot;nb-NO&quot; uiCulture=&quot;nb-NO&quot; /&gt;</code></p> <p><strong>Getting Http Header:</strong></p> <p><code id="questionViewInlineCode10-28133093-2">private string GetHeader(string key) <br> </code><pre class="prettyprint">{ var header = &quot;&quot;; if (Request.Headers.Contains(key))</pre><pre class="prettyprint">{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IEnumerable&lt;string&gt; values = new List&lt;string&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Request.Headers.TryGetValues(key, out values); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!string.IsNullOrEmpty(values.First())) header = values.First(); &nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; </pre><pre class="prettyprint">return header; <br /></pre><pre class="prettyprint">} </pre></p> <p><pre class="prettyprint">var headerUserName = GetHeader("UserName");<br /></pre></p> <p><pre class="prettyprint"><b>Request headers<br /></b><br />User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) <br />Chrome/26.0.1410.64 Safari/537.31 <br />UserName: &AElig;&Oslash;&Aring; <br />Accept: */* <br />Accept-Encoding: gzip,deflate,sdch <br />Accept-Language: en-US,en;q=0.8 <br />Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q<wbr>=0.3 </wbr></pre></p> <p><br> <b>Response headers</b><br> <br> Date: Wed, 15 May 2013 12:46:19 GMT <br> Server: Microsoft-IIS/6.0 <br> X-Powered-By: ASP.NET <br> X-AspNet-Version: 4.0.30319 <br> Cache-Control: no-cache <br> Pragma: no-cache <br> Expires: -1 <br> Content-Type: application/json; charset=utf-8 <br> Content-Length: 4174&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> 2013-05-20T11:09:16-04:002013-05-20T11:09:16.39-04:00urn:uuid:00000000-0000-0000-0000-000005399213http://forums.asp.net/p/1908101/5399213.aspx/1?The+remote+server+returned+an+error+404+Not+Found+The remote server returned an error: (404) Not Found. <p>i developed step definition for unit testing using this code .when i call wep api method using httpwebrequest It occuered&nbsp; this error The remote server returned an error: (404) Not Found.</p> <pre class="prettyprint">public void GivenIHaveFollowedCorrectStepsInUploadFile(Table table) { try { UploadFilesToRemoteUrl(&quot;http://localhost/api/File&quot;, new string[] { @&quot;C:\Project\New folder\FileHub\SourceCode_v2.5\SpecFlow\UploadDocument\Document.docx&quot; }, null); } }</pre> <pre class="prettyprint"> private void UploadFilesToRemoteUrl(string url, string[] files, string logpath) { long length = 0; string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url); httpWebRequest2.ContentType = "multipart/form-data;"; httpWebRequest2.Method = "POST"; httpWebRequest2.KeepAlive = true; //httpWebRequest2.Credentials = //System.Net.CredentialCache.DefaultCredentials; Stream memStream = new System.IO.MemoryStream(); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes(""); memStream.Write(boundarybytes, 0, boundarybytes.Length); length += boundarybytes.Length; string headerTemplate = @"Content-Disposition: form-data; name=\{0}\;filename =\{1}\\r\n Content-Type: application/octet-stream\r\n\r\n"; for (int i = 0; i &lt; files.Length; i++) { string header = string.Format(headerTemplate, "file" + i, files[i]); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); length += headerbytes.Length; FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { memStream.Write(buffer, 0, bytesRead); length += bytesRead; } memStream.Write(boundarybytes, 0, boundarybytes.Length); length += boundarybytes.Length; fileStream.Close(); } httpWebRequest2.ContentLength = memStream.Length; Stream requestStream = httpWebRequest2.GetRequestStream(); memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); requestStream.Close(); HttpWebResponse webResponse2 = (HttpWebResponse)httpWebRequest2.GetResponse(); Stream stream2 = webResponse2.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); webResponse2.Close(); httpWebRequest2 = null; webResponse2 = null; }</pre> <p></p> <p></p> 2013-05-20T03:23:40-04:002013-05-20T03:23:40.7-04:00urn:uuid:00000000-0000-0000-0000-000005398859http://forums.asp.net/p/1908003/5398859.aspx/1?Error+404Error 404 <p>I've got 2 projects: Core and Web.</p> <p>Core project has API controllers, models, etc. Web project has html pages only.</p> <p>I'm loading data from API using jQuery. When I do this exactrly from Core project (created a view), everything is ok. But when i do this from Web project, I've got error 404, but Fiddler shows evertthing is ok.</p> <p>Why is it so? What's a problem?</p> <p></p> 2013-05-19T06:14:47-04:002013-05-19T06:14:47.85-04:00urn:uuid:00000000-0000-0000-0000-000005398726http://forums.asp.net/p/1907959/5398726.aspx/1?MVC4+App+WebAPI+Security+Question+please+helpMVC4 App + WebAPI Security Question - please help Hi All, I have an existing MVC4 App using Forms Authentication. I am looking to add WebAPI Controllers to this application for common functionality which can then be shared to external clients. I would assume these clients would need Basic Authentication. However, I have read I am not meant to mix authentication in a project, but this has me confused. Is anyone able to show me the preferred method to authenticate a user on the both the MCV4 App via forms Authentication, but at same time allowing an external user to use the WebAPI from an external source? Thanks for the help. 2013-05-18T14:01:54-04:002013-05-18T14:01:54.46-04:00urn:uuid:00000000-0000-0000-0000-000005391019http://forums.asp.net/p/1905328/5391019.aspx/1?Need+to+check+is+user+is+authenticatedNeed to check is user is authenticated <p>I've added a web api app as an application under the default web site. The default website is a web forms application. This app uses authentication mode none. I'm currently using windows authentication on the web api, but I'd like to either have it use the same authentication as the root application or at least be able to check from within the web api app if the user is currently authenticated.</p> <p>Is there anyway for me to simply check that the user has already been authenticated? I have access to the database table with the login credentials so I could either</p> <ol> <li>redirect the user to the login page of the root app </li><li>send them to a view to enter their credentials </li></ol> <p>I know I really need to bite the bullet and sit down and learn the ins and outs of asp.net authentication and I WILL, but I need to meet a deadline for this particular application which I'm sure you can all sympathize with. Any help would be appreciated.</p> <p></p> 2013-05-11T09:55:14-04:002013-05-11T09:55:14.1-04:00urn:uuid:00000000-0000-0000-0000-000005395925http://forums.asp.net/p/1906491/5395925.aspx/1?What+is+Web+API+What is Web API..? <p>I am new to Asp.net WebAPI.Please anyone can help me about the Web API And what is it actually using..?</p> 2013-05-16T12:49:20-04:002013-05-16T12:49:20.81-04:00urn:uuid:00000000-0000-0000-0000-000005396066http://forums.asp.net/p/1906530/5396066.aspx/1?WebAPI+OData+reuse+types+when+adding+service+referenceWebAPI OData reuse types when adding service reference <p>I'm trying to use my WebApi with a .NET client and as such I'd like to reuse my existing entity classes as opposed to the ones generated by the service.</p> <p>In plain old WCF services checking the &quot;Reuse types in referenced assemblies&quot; checkbox in the Advanced view for Add Service reference allows me to do this, but for WebApi OData it seems to totally disregard the selection I made and generated brand new entities. Is there currently a way to accomplish this?</p> 2013-05-16T14:59:20-04:002013-05-16T14:59:20.563-04:00urn:uuid:00000000-0000-0000-0000-000005357893http://forums.asp.net/p/1896861/5357893.aspx/1?How+to+add+Session+to+Web+API+in+net+4+5How to add Session to Web API in .net 4.5 <p>This post&nbsp;<a href="http://forums.asp.net/post/4884822.aspx">http://forums.asp.net/post/4884822.aspx</a>&nbsp;explains how to add Session to Web API. &nbsp;However, in .net 4.5 webapi route is now registered with with&nbsp;Register(System.Web.Http.HttpConfiguration config) instead of&nbsp;RegisterRoutes(System.Web.Routing.RouteCollection routes).</p> <p>There is no RouteHandler to assign now. &nbsp;Anyone know how to add Session to the latest version of Web API? &nbsp;Yes, I have read all the reasons why you shouldn't do use session. &nbsp;My situtation still stands that it is needed.</p> 2013-04-09T18:37:16-04:002013-04-09T18:37:16.967-04:00urn:uuid:00000000-0000-0000-0000-000005395783http://forums.asp.net/p/1906450/5395783.aspx/1?xml+request+and+xml+response+for+a+php+clientxml request and xml response for a php client <p>Hi,</p> <p>This might a little bit confusing,but i will try to make it simple for the things that&nbsp;I wanted to know.</p> <p>Firstly,I have an untouchable php program with it's complete web services.The program web services are easier to be call when the&nbsp;client are also writen&nbsp;in php&nbsp;technologies.</p> <p>Secondly,I need to make a method to let the php webservice become compatible and easier to be use by asp client's.Is there any ways to do this?</p> <p>I did some researches and I found something to do with data contract and xml serialization writen in WCF.But no luck on the implementation on connecting to the webservice.php in the server across https://.</p> <p>If there are any ways to achieve this please share with me.Thank you ver much.</p> <p>&nbsp;</p> 2013-05-16T10:02:56-04:002013-05-16T10:02:56.69-04:00urn:uuid:00000000-0000-0000-0000-000005394483http://forums.asp.net/p/1906156/5394483.aspx/1?MVC+API+Return+custom+Error+message+as+XMLMVC API: Return custom Error message as XML <p>I have a basic controller that returns an XML response based on a custom class but will also throw an exception is the result is null</p> <p>i.e.&nbsp;</p> <pre class="prettyprint">private my_DevEntities db = new my_DevEntities(); // GET api/LicenceBasic/5 public Licence GetLicence(int id) { Licence myLicence = db.Licences.Single(a =&gt; a.licenceid == id); if (myLicence == null) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); } return myLicence; }</pre> <p>Instead of simply throwing a HTTP Status Code of 'NotFound'. I would like to return a more descriptive xml error response (this will be used for handling all errors not just the above example.</p> <p>i.e.</p> <pre class="prettyprint">&lt;myError&gt; &lt;error&gt;Invalid Response&lt;/error&gt; &lt;errorDesc&gt;No licences found&lt;/errorDesc&gt; &lt;/myError&gt;</pre> <p>I have created a custom class i.e.</p> <pre class="prettyprint"> public class ErrorResponse { public string error { get; set; } public string errorDesc { get; set; } }</pre> <p>But&nbsp;can not see how to return that in the HTTPResponse (or any other way?).</p> <p>Can anyone offer any pointers?</p> <p>Thanks.<br> <br> <br> </p> 2013-05-15T09:30:45-04:002013-05-15T09:30:45.8-04:00urn:uuid:00000000-0000-0000-0000-000005388204http://forums.asp.net/p/1904695/5388204.aspx/1?Where+Can+i+find+best+example+for+HttpPUT+method+Where Can i find best example for HttpPUT method? <p>Hello,</p> <p>I want to insert Student Information using Web API through HttpPUT method and return student reference number(in string format). I am new to web api development.</p> <p>Can you help me in my issue.</p> <p>Thanks in advance,</p> <p>Venkata.</p> 2013-05-08T14:33:25-04:002013-05-08T14:33:25.743-04:00urn:uuid:00000000-0000-0000-0000-000005394027http://forums.asp.net/p/1906059/5394027.aspx/1?WebAPI+html+select+multiple+and+knockout+jsWebAPI html select multiple and knockout.js <p>I'm trying to find an example of how to use WebAPI with knockout.js and a multiple select list box.</p> <p>I have not been able to get much farther than hardcoding a list of items in an observableArray in order to get them in the list box. &nbsp;Now I am trying to find out how to get those items from a database through WebAPI and into the list box and then how to know which items were selected and how to pass and use those items in a WebAPI call.</p> <p>Does anyone have some resources that they have come accross to show how to do this type of thing?</p> 2013-05-15T00:40:55-04:002013-05-15T00:40:55.337-04:00