Tag: asp.net web api

WebAPI Put返回HTTPResponseMessage null

我需要实现简单的编辑function。我正在使用webapi服务来更新我的测试对象。 我从控制器发布请求中调用以下方法。 这是在调用put服务时调用metod的控制器 public ActionResult TestEdit(Test test) { if (ModelState.IsValid) { // objTest is returned null HttpResponseMessage objtest = TestDatabaseService.TestEdit(test.testID, test); } } // Method which calls put service testDataService public HttpResponseMessage TestEdit(int id, Test test)** { string uri = baseUri + “Test/” + id; using (HttpClient httpClient = new HttpClient()) { Task response = […]

Web api使CORS无法正常工作

我无法使用ajax访问asp.net-web-api中的资源。 我为所有请求启用了cors并添加了启用cors属性,但仍然出现错误: No ‘Access-Control-Allow-Origin’ header is present …我也尝试过特定网站,但仍然是同样的错误。 我正在使用Visual Studio 2013 .Net Framework 4.5。 我可以通过与asp.net-web-api在同一网站上的html访问资源 码: HTML : function GetAllStudents() { $.ajax({ type: “GET”, url: “http://localhost/studentsWebapi/students”, success: function(result) { var list = $(“#students”); for (var i = 0; i < result.length; i++) { var students = result[i]; list.append('’ + student.FirstName + ”); } }, error: […]

返回HttpResponseMessage时的WebAPI Gzip

我有一个WebAPI控制器,它返回一个HttpResponseMessage ,我想添加gzip压缩。 这是服务器代码: using System.Net.Http; using System.Web.Http; using System.Web; using System.IO.Compression; [Route(“SomeRoute”)] public HttpResponseMessage Post([FromBody] string value) { HttpContext context = HttpContext.Current; context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress); HttpContext.Current.Response.AppendHeader(“Content-encoding”, “gzip”); HttpContext.Current.Response.Cache.VaryByHeaders[“Accept-encoding”] = true; return new SomeClass().SomeRequest(value); } 这是使用jquery的ajax调用的客户端代码: $.ajax({ url: “/SomeRoute”, type: “POST”, cache: “false”, data: SomeData, beforeSend: function (jqXHR) { jqXHR.setRequestHeader(‘Accept-Encoding’, ‘gzip’); }, success: function(msg) […]

VS Web Web应用程序中的ASP.NET Web Api CRUD操作

我试图在VS 2010 Web应用程序中进行ASP.NET Web Api CRUD操作,但为什么结果不会从源表返回所有整行。 这是我的代码: 路线/ Globax.asax protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, // browse with localhost:7031/api/product //routeTemplate: “{controller}/{id}”, // browse with localhost:7031/product defaults: new { id = System.Web.Http.RouteParameter.Optional } ); Controller / ProductController.cs: public class ProductController : ApiController { NorthwindEntities db = new NorthwindEntities(); public List […]

如何在web api控制器中的json嵌套对象中发送数组括号

我将数据发送到旅行门户的其他API。 根据他们的文档,json数据必须采用以下格式 { “EndUserIp”: “192.168.10.10”, “TokenId”: “ac2751e9-4cc3-406f-b678-c947e4f57a00”, “AdultCount”: “1”, “ChildCount”: “0”, “InfantCount”: “0”, “DirectFlight”: “false”, “OneStopFlight”: “false”, “JourneyType”: “1”, “PreferredAirlines”: null, “Segments”: [ { “Origin”: “DEL”, “Destination”: “BOM”, “FlightCabinClass”: “1”, “PreferredDepartureTime”: “2015-11-06T00: 00: 00”, “PreferredArrivalTime”: “2015-11-06T00: 00: 00” }], “Sources”: [ “6E” ] } 我的模特是 public class otherType { public string Origin { get; set; […]

jQuery ajax加载问题与MVC控制器的参数

介绍 我正在使用带有WebApi 2.0和jQuery的ASP.NET MVC 5。 问题 我正在尝试显示一个模式,其中包含来自ajax调用的用户详细信息。 使用$.getJSON()我成功地收到了来自WebApi的Json格式的用户详细信息。 现在我想使用局部视图在我的模态中加载html。 我尝试使用以下代码,但html没有加载。 在我的控制台中,我看到了带有正确数据和html的请求和响应。 loadItem: function () { var $a = $(this); var id = $a.attr(‘data-id’); $.getJSON(uri_api + ‘/’ + id) .done(function (data) { console.log(JSON.stringify(data)); $.ajaxSetup({ contentType: ‘application/json; charset=utf-8’, dataType: ‘json’, cache: false }); // Get the partial view to show in modal $.ajax({ type: ‘POST’, data: JSON.stringify(data), […]

ASP.NET Web API身份validation选项

有哪些选项可用于对来自其他域的JQuery应用程序使用的MVC3 Web API应用程序进行身份validation? 这是迄今为止我尝试过的约束/事情: – 我不想使用OAuth; 对于用户群有限的私有应用程序,我不能指望最终用户在现有提供商上拥有自己的帐户,并且无法实现自己的帐户 我有一个function齐全的HMAC-SHA256实现,可以正常使用标头中传递的数据; 但这在IE中不起作用,因为IE8 / 9中的CORS已损坏,并且不允许您发送标头 我需要跨域,因为消费应用程序在API的不同域上,但不能使用jsonp,因为它不允许您使用标头 我想避免使用基于令牌(仅)的方法,因为这可以重播并通过状态来违反REST 此时我已经退回到HMAC-SHA256方法,该方法使用URL或查询字符串/ post来提供哈希和其他变量。 将这些变量放在URL中似乎很脏,将它们放在查询字符串/post中是一件痛苦的事。 我成功地使用了JQuery $ .ajaxSetup beforeSend选项来生成哈希并将其附加到标题,但正如我所提到的,你不能使用IE8 / 9的标题。 现在我不得不求助$ .ajaxPrefilter,因为我无法更改beforeSend中的ajax数据,并且不能只扩展$ .ajaxSetup中的数据,因为我需要根据ajax的类型动态计算散列值查询。 $ .ajaxPrefilter也是一个问题,因为没有干净/简单的方法以方法无关的方式添加所需的变量…即它必须是GET的查询字符串和POST的formdata 我必须遗漏一些东西,因为我找不到一个解决方案: – a)支持跨域a)不是MVC和JQuery方面的大规模黑客攻击c)实际上安全d)适用于IE8 / 9 必须有人在那里正确地做这件事…… 编辑 为了澄清,API方面的身份validation机制很好……无论我validation请求的哪种方式,我生成一个GenericPrincipal并在API中使用它(这个的优点是另一个post,但它确实允许我使用MVC中的标准授权机制,我更倾向于自己编写…而不是我的API上的其他开发人员学习和维护 问题主要在于将身份validation信息从客户端传输到API: – – 它不能依赖于服务器/ API状态。 所以我不能在一次调用中传递用户名/密码,获得一个令牌然后继续使用该令牌(打开重放攻击) – 任何需要使用请求头的东西都没有,因为IE使用XDR而不是XHR浏览器,它不支持自定义标头(我知道IE10支持XHR,但实际上我需要IE8 +支持) – 我想我已经卡住生成一个HMAC并将其传递到URL某处(路径或查询字符串)但这个看起来像是一个黑客,因为我正在使用不是为此设计的请求的部分 – 如果我使用路径有很多杂乱的解析,因为至少我必须传递用户名,时间戳和哈希与每个请求; 这些需要以某种方式分隔,我几乎无法控制在其余url中使用的分隔符 – 如果我使用数据(querystring / formdata)我需要更改我发送validation详细信息的位置,具体取决于我的方法使用(用于POST / PUT […]

ASP.NET WEBAPI文件上传,IE9问题

我在代码下面使用ASP.NET WEBAPI创建了一个文件上传方法: [DataContract] public class FileDesc { [DataMember] public string name { get; set; } [DataMember] public string url { get; set; } [DataMember] public long size { get; set; } [DataMember] public string UniqueFileName { get; set; } [DataMember] public int id { get; set; } [DataMember] public DateTime modifiedon { get; set; } […]

如何在Web API Controller上返回Json对象

我在我的asp.net控制器上使用下面的代码在javascript上返回我的Ajax上的Json对象 public JsonResult myMethod() { // return a Json Object, you could define a new class return Json(new { Success = true, //error Message = “Success” //return exception }); } jQuery的阿贾克斯: $.ajax({ type: “POST”, url: url_ , data: search, success: function(data) { //Show Json Properties from Controller ( If Success == false show exception Message […]

DELETE命令未访问WebAPI控制器

在通过ASP.NET Web API提交请求时,我很难在我的Controller上获取DELETE方法。 它返回404,但我无法弄清楚原因。 GET和POST请求按预期工作,返回项目列表以及提供id时的单个项目,但是当我使用DELETE请求调用API时,我得到404错误。 场景: 1. ASP.NET Web窗体应用程序…… 虽然我已经安装了MVC4软件包以利用Web APIfunction,但它不是MVC应用程序。 2. global.asax中的路由表定义 RouteTable.Routes.MapHttpRoute( “Default”, “api/{controller}/{id}”, new { id = RouteParameter.Optional } ); 3.控制器定义 public HttpResponseMessage Post(Customer customer) { CustomerDb.Customers.AddObject(customer); CustomerDb.SaveChanges(); var response = new HttpResponseMessage(customer, HttpStatusCode.Created); response.Headers.Location = new Uri(Request.RequestUri, “/api/Customer/”+customer.id.ToString()); return response; } public CustomerDTO Get(int id) { CustomerDTO custDTO = null; Customer […]