Android Volley POST string in body

To send a normal POST request (no JSON) with parameters like username and password, you’d usually override getParams() and pass a Map of parameters: public void HttpPOSTRequestWithParameters() { RequestQueue queue = Volley.newRequestQueue(this); String url = “http://www.somewebsite.com/login.asp”; StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(“Response”, response); } }, … Read more

JSON Deserialization – String Is Automatically Converted To Int

This is a feature of Json.NET: when deserializing a primitive type, it will convert the primitive JSON value to the target c# type whenever possible. Since the string “4” can be converted to an integer, deserialization succeeds. If you don’t want this feature, you can create a custom JsonConverter for integral types that checks that … Read more

Changing the parameter name Web Api model binding

You can use the Name property of the FromUri binding attribute to use query string parameters with different names to the method arguments. If you pass simple parameters rather than your QueryParameters type, you can bind the values like this: /api/values/5?cap=somecap&id=1 public IHttpActionResult GetValue([FromUri(Name = “cap”)] string capabilities, int id) { }

WebApi Help Page: don’t escape HTML in XML documentation

In the installed XmlDocumentationProvider.cs file at Areas\HelpPage, you can look for a method called GetTagValue. Here modify the return value from node.Value.Trim() to node.InnerXml. private static string GetTagValue(XPathNavigator parentNode, string tagName) { if (parentNode != null) { XPathNavigator node = parentNode.SelectSingleNode(tagName); if (node != null) { return node.InnerXml; } } return null; } Now open … Read more

ASP.NEt MVC using Web API to return a Razor view

You could send an HTTP request to your Web API controller from within the ASP.NET MVC controller: public class ProductController : Controller { public ActionResult Index() { var client = new HttpClient(); var response = client.GetAsync(“http://yourapi.com/api/products”).Result; var products = response.Content.ReadAsAsync<IEnumerable<Product>>().Result; return View(products); } } Also if you can take advantage of the .NET 4.5 async/await … Read more

Problems implementing ValidatingAntiForgeryToken attribute for Web API with MVC 4 RC

You could try reading from the headers: var headers = actionContext.Request.Headers; var cookie = headers .GetCookies() .Select(c => c[AntiForgeryConfig.CookieName]) .FirstOrDefault(); var rvt = headers.GetValues(“__RequestVerificationToken”).FirstOrDefault(); AntiForgery.Validate(cookie != null ? cookie.Value : null, rvt); Note: GetCookies is an extension method that exists in the class HttpRequestHeadersExtensions which is part of System.Net.Http.Formatting.dll. It will most likely exist in … Read more

MVC Web API not working with Autofac Integration

ASP.Net Wep.API and ASP.NET MVC uses two different IDependencyResolver (because they designed the Wep.API to not depend on ASP.NET MVC) so you need to setup both: var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); So the AutofacDependencyResolver is needed to inject decencies to regular MVC Controller derived controllers. And the AutofacWebApiDependencyResolver is needed … Read more

WebAPI CORS with Windows Authentication – allow Anonymous OPTIONS request

I used self-hosting with HttpListener and following solution worked for me: I allow anonymous OPTIONS requests Enable CORS with SupportsCredentials set true var cors = new EnableCorsAttribute(“*”, “*”, “*”); cors.SupportsCredentials = true; config.EnableCors(cors); var listener = appBuilder.Properties[“System.Net.HttpListener”] as HttpListener; if (listener != null) { listener.AuthenticationSchemeSelectorDelegate = (request) => { if (String.Compare(request.HttpMethod, “OPTIONS”, true) == 0) … Read more