asp.net-web-api

GlobalConfiguration.Configure() not present after Web API 2 and .NET 4.5.1 migration

ε祈祈猫儿з 提交于 2019-12-28 03:23:10
问题 I recently started following this guide to migrate my project to .NET 4.5.1 and Web Api 2. The very first thing MS developer Rick Anderson asks you to do is change: WebApiConfig.Register(GlobalConfiguration.Configuration); to GlobalConfiguration.Configure(WebApiConfig.Register); in the global.asax file. Yet this is giving me an error when I try to build: Error 1 'System.Web.Http.GlobalConfiguration' does not contain a definition for 'Configure' My project is currently on MVC 5 and Web Api 2

Return HTML from ASP.NET Web API

二次信任 提交于 2019-12-28 03:20:31
问题 How to return HTML from ASP.NET MVC Web API controller? I tried the code below but got compile error since Response.Write is not defined: public class MyController : ApiController { [HttpPost] public HttpResponseMessage Post() { Response.Write("<p>Test</p>"); return Request.CreateResponse(HttpStatusCode.OK); } } 回答1: ASP.NET Core. Approach 1 If your Controller extends ControllerBase or Controller you can use Content(...) method: [HttpGet] public ContentResult Index() { return base.Content("

User Authentication in ASP.NET Web API

我是研究僧i 提交于 2019-12-28 03:15:28
问题 This topic has been incredibly confusing for me. I am a rookie in HTTP apps but need to develop an iPhone client that consumes JSON data from somewhere. I chose Web API from MS because it seemed easy enough but when it comes to authenticating users, things get quite frustrating. I am amazed how I've not been able to find a clear example of how to authenticate an user right from the login screen down to using the Authorize attribute over my ApiController methods after several hours of Googling

Is there a way in Json.NET serialization to distinguish between “null because not present” and “null because null”?

蹲街弑〆低调 提交于 2019-12-28 02:56:04
问题 I'm working in an ASP.NET webapi codebase where we rely heavily on the automatic support for JSON deserialization of message bodies into .NET objects via JSON.NET. As part of building out patch support for one of our resources, I'd very much like to distinguish between an optional property in the JSON object that's not present, vs. that same property that's explicitly to null. My intention is to use the first for "don't change what's there" vs. "delete this thing." Does anyone know if it's

POST string to ASP.NET Web Api application - returns null

故事扮演 提交于 2019-12-27 17:30:51
问题 Im trying to transmit a string from client to ASP.NET MVC4 application. But I can not receive the string, either it is null or the post method can not be found (404 error) Client Code to transmit the string (Console Application): HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:49032/api/test"); request.Credentials = new NetworkCredential("user", "pw"); request.Method = "POST"; string postData = "Short test..."; byte[] byteArray = Encoding.UTF8.GetBytes(postData);

POST string to ASP.NET Web Api application - returns null

∥☆過路亽.° 提交于 2019-12-27 17:30:06
问题 Im trying to transmit a string from client to ASP.NET MVC4 application. But I can not receive the string, either it is null or the post method can not be found (404 error) Client Code to transmit the string (Console Application): HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:49032/api/test"); request.Credentials = new NetworkCredential("user", "pw"); request.Method = "POST"; string postData = "Short test..."; byte[] byteArray = Encoding.UTF8.GetBytes(postData);

JSON.NET as a WebAPI 2 OData serializer vs ODataMediaTypeFormatter

喜夏-厌秋 提交于 2019-12-27 14:57:59
问题 I'm trying to use JSON.NET as a default serializer in WebAPI 2 stack. I've implemented JsonMediaTypeFormatter, in which I've used JSON.NET serializer for serialize/deserialize data and created JsonContentNegotiator for using this media type formatter. All works fine except OData querying - if I add [Queryable] metadata ot action method, then response object doesn't contains any metadata information, only list of entities. Small example. My action method: [Queryable] public async Task

JSON.NET as a WebAPI 2 OData serializer vs ODataMediaTypeFormatter

最后都变了- 提交于 2019-12-27 14:56:26
问题 I'm trying to use JSON.NET as a default serializer in WebAPI 2 stack. I've implemented JsonMediaTypeFormatter, in which I've used JSON.NET serializer for serialize/deserialize data and created JsonContentNegotiator for using this media type formatter. All works fine except OData querying - if I add [Queryable] metadata ot action method, then response object doesn't contains any metadata information, only list of entities. Small example. My action method: [Queryable] public async Task

Handle ModelState Validation in ASP.NET Web API

强颜欢笑 提交于 2019-12-27 10:32:07
问题 I was wondering how I can achieve model validation with ASP.NET Web API. I have my model like so: public class Enquiry { [Key] public int EnquiryId { get; set; } [Required] public DateTime EnquiryDate { get; set; } [Required] public string CustomerAccountNumber { get; set; } [Required] public string ContactName { get; set; } } I then have a Post action in my API Controller: public void Post(Enquiry enquiry) { enquiry.EnquiryDate = DateTime.Now; context.DaybookEnquiries.Add(enquiry); context

Java finished with non-zero exit value 2 - Android Gradle

谁说我不能喝 提交于 2019-12-27 08:15:06
问题 I'm getting this error executing my Android app (I cleaned it and then built it, but the error is still present) Sync: OK Make Project: OK Clean: OK Run: Error Error:Execution failed for task ':app:dexDebug' .com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_25\bin\java.exe'' finished with non-zero exit value 2 My gradle file: apply plugin: 'com.android.application' android { compileSdkVersion 21