使用较新的ASP.NET Web API,在Chrome中我看到了XML - 如何更改它以请求JSON,以便我可以在浏览器中查看它?我相信它只是请求标题的一部分,我是否正确?
当你有服务器端代码(即一些ApiController)并且你的函数是异步的 - 所以它们返回Task<SomeObject>- 你认为最好的做法是等待你调用的函数ConfigureAwait(false)吗?
我已经读过它更高效,因为它不必将线程上下文切换回原始线程上下文.但是,使用ASP.NET Web Api,如果您的请求是在一个线程上进行的,并且等待某些函数和调用ConfigureAwait(false),则可能会在返回ApiController函数的最终结果时将您置于不同的线程上.
我在下面输入了一个我正在谈论的例子:
public class CustomerController : ApiController
{
public async Task<Customer> Get(int id)
{
// you are on a particular thread here
var customer = await SomeAsyncFunctionThatGetsCustomer(id).ConfigureAwait(false);
// now you are on a different thread! will that cause problems?
return customer;
}
}
Run Code Online (Sandbox Code Playgroud) 我花了几个月的时间试图掌握WCF背后的概念,最近我开发了我的第一个WCF服务应用程序.
我很难理解配置文件中的所有设置.
我不相信环境,但似乎你可以用它做出惊人的东西.
前几天我发现微软已经推出了一款名为ASP.NET Web API的新产品.
我可以阅读它是一个RESTful框架,非常易于使用和实现.
现在,我试图找出2个框架之间的主要区别,以及是否应该尝试使用新API 转换旧的 WCF服务应用程序.
有人可以帮我理解每个的差异和用法吗?
我读到,当您无法访问Web服务器的标头时,您可以使用以下命令关闭缓存:
<meta http-equiv="Cache-Control" content="no-store" />
Run Code Online (Sandbox Code Playgroud)
但我也读到这在某些版本的IE中不起作用.是否有任何<meta>标签可以关闭所有浏览器中的缓存?
我有一个ASP.NET Web API(版本4)REST服务,我需要传递一个整数数组.
这是我的行动方法:
public IEnumerable<Category> GetCategories(int[] categoryIds){
// code to retrieve categories from database
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的URL:
/Categories?categoryids=1,2,3,4
Run Code Online (Sandbox Code Playgroud) 我想使用ASP.NET Web API 构建RESTful Web服务,第三方开发人员将使用它来访问我的应用程序的数据.
我已经阅读了很多关于OAuth的内容,它似乎是标准的,但找到一个很好的样本,文档解释它是如何工作的(实际上确实有效!)似乎非常困难(特别是对于OAuth的新手).
是否有实际构建和工作的示例,并说明如何实现它?
我已经下载了很多样本:
我还看过一些博客,建议一个简单的基于令牌的方案(像这样) - 这似乎重新发明了轮子,但它确实具有概念上相当简单的优势.
似乎在SO上有很多这样的问题,但没有好的答案.
每个人在这个领域做什么?
我对我们向客户返回错误的方式感到担忧.
当我们收到错误时,我们通过抛出HttpResponseException立即返回错误:
public void Post(Customer customer)
{
if (string.IsNullOrEmpty(customer.Name))
{
throw new HttpResponseException("Customer Name cannot be empty", HttpStatusCode.BadRequest)
}
if (customer.Accounts.Count == 0)
{
throw new HttpResponseException("Customer does not have any account", HttpStatusCode.BadRequest)
}
}
Run Code Online (Sandbox Code Playgroud)
或者我们累积所有错误然后发送回客户端:
public void Post(Customer customer)
{
List<string> errors = new List<string>();
if (string.IsNullOrEmpty(customer.Name))
{
errors.Add("Customer Name cannot be empty");
}
if (customer.Accounts.Count == 0)
{
errors.Add("Customer does not have any account");
}
var responseMessage = new HttpResponseMessage<List<string>>(errors, HttpStatusCode.BadRequest);
throw new HttpResponseException(responseMessage);
}
Run Code Online (Sandbox Code Playgroud)
这只是一个示例代码,无论是验证错误还是服务器错误都无关紧要,我只想了解最佳实践,每种方法的优缺点.
我希望将ASP.NET Web API添加到Visual Studio 2012中开发的ASP.NET MVC 4 Web应用程序项目中.我必须执行哪些步骤才能将正常运行的Web API添加到项目中?我知道我需要一个从ApiController派生的控制器,但这就是我所知道的.
如果我需要提供更多详细信息,请与我们联系.
asp.net-mvc asp.net-mvc-4 asp.net-web-api visual-studio-2012
我一直在玩ASP.NET MVC 4 beta,我现在看到两种类型的控制器:ApiController和Controller.
我对可以选择特定控制器的情况感到困惑.
例如:如果我想返回一个视图,那么我要使用ApiController还是普通的Controller?我知道WCF Web API现在已与MVC集成.
从现在我们可以使用两个控制器可以有人请指出在哪种情况下去相应的控制器.
asp.net-web-api ×10
c# ×5
asp.net-mvc ×3
rest ×2
arrays ×1
async-await ×1
caching ×1
firefox ×1
html ×1
imdb ×1
json ×1
oauth ×1
wcf ×1