我使用 WebApi 和 Client 开发服务,这些服务通过 POST 方法(以节省带宽)发送到该服务的 gzipped 数据。他们俩都在我的控制之下。在服务器上,我收到压缩数据,解压缩它并有如下字符串:
section[0][caption]=Foo&
section[0][address]=175896&
section[0][counters][]=2&
section[0][counters][]=2&
section[0][errors][]=ERR_NOT_AVAILABLE&
errors=true&
trmtimestamp=1346931864358
Run Code Online (Sandbox Code Playgroud)
即简单的 www-form-urlencoded 字符串。
ASP.NET MVC4 WebApi 是否有一些方法可以将此字符串绑定或反序列化到我的模型?
section[0][caption]=Foo&
section[0][address]=175896&
section[0][counters][]=2&
section[0][counters][]=2&
section[0][errors][]=ERR_NOT_AVAILABLE&
errors=true&
trmtimestamp=1346931864358
Run Code Online (Sandbox Code Playgroud) 我是 OData 的新手。
因此,我非常密切地关注本教程。( http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/getting-started-with-odata-in-web-api/create-a-read-only -odata-端点)
但是,在教程的第二部分(http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/getting-started-with-odata-in-web- api/explore-the-odata-endpoint),当我尝试http://localhost:52868/odata/products.svc在 fiddler 或浏览器中浏览时,出现 404 错误。
我安装的 OData 包是这个http://www.nuget.org/packages/microsoft.aspnet.webapi.odata
这个包是18号刚发布的,不知道自从教程发布差不多一个月后有没有什么变化
我有返回产品列表的 api 方法:getAllProduct()返回填充列表,包括:
List<Product> dependProduct
Run Code Online (Sandbox Code Playgroud)
但客户端收到一个空的dependProduct.
public class Product
{
public string Title { get; set; }
public int Cost { get; set; }
public List<Product> dependProduct = new List<Product>();
}
Run Code Online (Sandbox Code Playgroud)
控制器:
[Route("~/Shop/Product")]
[ResponseType(typeof(IEnumerable<Product>))]
public HttpResponseMessage Get()
{
var data = getAllProduct(); // has dependProduct
return this.Request.CreateResponse(HttpStatusCode.OK, data);
}
private List<Product> getAllProduct()
{
return context.Products.ToList();
}
Run Code Online (Sandbox Code Playgroud)
客户:
var request = new RestRequest("/Shop/Product", Method.GET);
var response = client.Execute<List<Product>>(request);
return response.Data; // has not dependProduct why?
Run Code Online (Sandbox Code Playgroud) 我有一些信息需要实时显示给用户。在我重复调用服务器以获取新数据之前。
现在我想用 SignalR 替换这些重复的调用。一般想法是在客户端订阅集线器,当新条目添加到数据库时,集线器将向所有客户端发送更新,但是我坚持使用该结构的一般设计。
所以我有我的 WebAPI 项目和服务层。在添加新条目时的服务中,我将引发一个事件,并在集线器内部捕获该事件并将更新发送给所有客户端。
使用 SignalR 实现这些事情的方式是否正确?鉴于我正在使用 asp.net WebAPI 和 SimpleInjector,有人可以解释我如何实现它。
我有返回某个对象的可枚举列表的 Web Api 服务,但是我在客户端看到每个对象都有一些额外的 $id ?为什么我得到它?是什么原因 ?是否可以停用它?
我有一个正在 Visual Studio 2013 中处理的 Web API 项目,我希望我的控制器通过查询字符串接受逗号分隔的值列表,类似于:
http://localhost:12345/api/Procedures/1?embed=doctors,drugs&fields=fieldA,fieldB,fieldC
Run Code Online (Sandbox Code Playgroud)
这样做的原因是我希望能够控制是否使用embed参数通过自定义嵌入查询相关资源(附加表),并使用fields参数控制从基础对象返回的字段。
我在谷歌上做了一些搜索,但建议的大部分内容都与扩展 IModelBinder ( http://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-理解绑定从 uri/)或设置自定义 ActionFilterAttribute (转换自定义操作过滤器以供 Web API 使用?)这对于相对简单的事情来说似乎有点过分。
仅供参考,我正在使用实体框架 dbContext 类连接到我的数据库。
我正在尝试学习/扩展我对 .NET MVC/REST/API 和 Web-sockets/SignalR 的知识。为此,我正在实施一个聊天应用程序。
我有一个典型的 MVC 介绍页面,它在表单中获取用户名和电子邮件地址,并将该数据提交到 RESTFul API,然后将新用户添加到数据库中。
<form class="form-horizontal" role="form" action="/api/GroopsAPIUsers" method="POST">
Run Code Online (Sandbox Code Playgroud)
在该页面(?)的控制器内部,我将用户重定向到一个页面,在那里他们可以选择他们想要进入的房间。
public HttpResponseMessage Post( GroopsUser userValue)
{
userValue.ID = Guid.NewGuid();
bool results = groopsRepository.AddNewUser(userValue);
// return results;
var response = Request.CreateResponse(HttpStatusCode.Redirect);
//from http://stackoverflow.com/questions/11324711/redirect-from-asp-net-web-api-post-action
string fullyQualifiedUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority);
response.Headers.Location = new Uri (fullyQualifiedUrl + "/home/rooms/?userID=" + userValue.ID);
return response;
}
Run Code Online (Sandbox Code Playgroud)
但这感觉不对。似乎 API 应该只执行 CRUD 操作,而与用户重定向到的页面无关。
这是错误的方法吗?如果是这样,有人可以指出我正确的方向吗?(我不确定我是否正确使用了所有这些术语)
...格雷戈里
我想知道如何在 RESTful API 中建立事务安全性,其中一切都围绕单个实体构建。
数据库模型:
用户在浏览器中执行的步骤:
提出的要求:
PATCH/PUT发票数据/订单号。POST 物品。DELETE 物品。PATCH/PUT项。如果在上述任何请求之后发生错误,进一步的调用可能会破坏数据完整性。此外,必须撤销先前的请求。例如,如果删除项目失败,则必须重新执行步骤 1 和 2,以使整个发票与以前一样。
另一个可能出现的问题是浏览器崩溃、互联网连接中断、服务器故障或其他任何问题。
如何确保在某种事务中执行某些操作以维护数据完整性和安全性?
我在客户端使用 Web Api (C#) 和 angular.js。我需要下载服务器响应内容(zip 的 ByteArrayContent)。我在服务器上有这个方法:
public HttpResponseMessage Download(DownloadImagesInput input)
{
if (!string.IsNullOrEmpty(input.ImageUrl))
{
byte[] imageBytes = GetByteArrayFromUrl(input.ImageUrl);
ZipManager manager = new ZipManager();
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
byte[] zipBytes;
zipBytes = string.IsNullOrEmpty(input.QrCode) ? manager.ZipFiles(imageBytes)
: manager.ZipFiles(imageBytes, input.QrCode);
result.Content = new ByteArrayContent(zipBytes);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/zip");
return result;
}
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
Run Code Online (Sandbox Code Playgroud)
ZipManager 是我的服务,它只返回 zip 文件的字节数组。我需要在客户端下载这个 zip 存档。这是我的客户:
$apiService.downloadZip({ 'ImageUrl': $scope.currentImage, 'QrCode': str }).then(function (response) {
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:application/zip,' + response.data;
hiddenElement.target = …Run Code Online (Sandbox Code Playgroud) 我正在使用NDepend进行代码分析,并收到以下警告:
https://www.ndepend.com/default-rules/NDepend-Rules-Explorer.html?ruleid=ND1901#!
此规则警告未声明为只读的静态字段。
在面向对象编程中,实例字段是用于保存可修改状态的自然工件。这种可变的静态字段会在运行时引起对预期状态的混淆,并损害代码的可测试性,因为对于每个测试都重复使用相同的可变状态。
我的代码如下:
using Cosmonaut;
using Microsoft.Azure.Documents.Client;
using System.Configuration;
using LuloWebApi.Entities;
namespace LuloWebApi.Components
{
/// <summary>
/// Main class that encapsulates the creation of instances to connecto to Cosmos DB
/// </summary>
public sealed class CosmosStoreHolder
{
/// <summary>
/// Property to be initiated only once in the constructor (singleton)
/// </summary>
private static CosmosStoreHolder instance = null;
/// <summary>
/// To block multiple instance creation
/// </summary>
private static readonly object padlock = new object();
/// …Run Code Online (Sandbox Code Playgroud) asp.net-web-api ×10
asp.net ×5
c# ×5
asp.net-mvc ×4
rest ×3
.net ×1
angularjs ×1
javascript ×1
odata ×1
parameters ×1
signalr ×1
web-services ×1