鉴于以下webapiconfig;
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
和这个控制器;
public class ProductsController : ApiController
{
Product[] _products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts() …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc asp.net-mvc-routing wcf-web-api asp.net-web-api
使用新的WCF Web API而不是ASP.NET MVC 3来公开轻量级JSON Web服务层有什么优势?我在很多方面都喜欢Web API,但缺点是它不能在单声道上工作,而MVC 3则可以.这两种方法有哪些主要区别?
我正在尝试使用HttpClient需要基本HTTP身份验证的第三方服务.我正在使用AuthenticationHeaderValue.这是我到目前为止所提出的:
HttpRequestMessage<RequestType> request =
new HttpRequestMessage<RequestType>(
new RequestType("third-party-vendor-action"),
MediaTypeHeaderValue.Parse("application/xml"));
request.Headers.Authorization = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "username", "password"))));
var task = client.PostAsync(Uri, request.Content);
ResponseType response = task.ContinueWith(
t =>
{
return t.Result.Content.ReadAsAsync<ResponseType>();
}).Unwrap().Result;
Run Code Online (Sandbox Code Playgroud)
看起来POST动作工作正常,但我没有收到我期望的数据.通过一些试验和错误,并最终使用Fiddler来嗅探原始流量,我发现授权标头没有被发送.
我已经看过了,但我认为我已经将指定的身份验证方案指定为AuthenticationHeaderValue构造函数的一部分.
有没有我错过的东西?
我有一个服务接口,其方法具有类型的参数Stream.我应该在读完此流中的所有数据后关闭流,还是在方法调用完成后由WCF运行时完成?
我见过的大多数例子,只读取流中的数据,但不要在Stream上调用Close或Dispose.
通常我会说我不必关闭流因为类不是流的所有者,但原因是为什么问这个问题是我们正在调查我们的系统中的一个问题,一些Android客户端,那个使用HTTP-Post将数据发送到此服务有时具有未关闭的打开连接(使用netstat哪个列表ESTABLISHED Tcp连接进行分析).
[ServiceContract]
public interface IStreamedService {
[OperationContract]
[WebInvoke]
Stream PullMessage(Stream incomingStream);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, UseSynchronizationContext = false)]
public class MyService : IStreamedService {
public System.IO.Stream PullMessage(System.IO.Stream incomingStream) {
// using(incomingStream) {
// Read data from stream
// }
Stream outgoingStream = // assigned by omitted code;
return outgoingStream;
}
Run Code Online (Sandbox Code Playgroud)
服务/绑定的配置
<webHttpBinding>
<binding name="WebHttpBindingConfiguration"
transferMode="Streamed"
maxReceivedMessageSize="1048576"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
closeTimeout="00:10:00"/>
</webHttpBinding>
Run Code Online (Sandbox Code Playgroud) 我想使用Castle Windsor在WebApi应用程序中实现依赖注入.我有以下示例代码 -
界面 -
public interface IWatch
{
{
DateTime GetTime();
}
}
Run Code Online (Sandbox Code Playgroud)
以下Watch类实现了IWatch接口 -
public class Watch:IWatch
{
public DateTime GetTime()
{
return DateTime.Now;
}
}
Run Code Online (Sandbox Code Playgroud)
WebApi控制器 - WatchController如下 -
public class WatchController : ApiController
{
private readonly IWatch _watch;
public WatchController()
{
_watch = new Watch();
}
//http://localhost:48036/api/Watch
public string Get()
{
var message = string.Format("The current time on the server is: {0}", _watch.GetTime());
return message;
}
}
Run Code Online (Sandbox Code Playgroud)
目前我在WatchController构造函数中使用Watch启动IWatch对象.我想删除使用Windsor Castle依赖注入原理在构造函数中初始化IWatch的依赖性.
在这种WebApi的情况下,任何人都可以为我提供实现依赖注入的步骤吗?提前致谢!
我是WCF RESTFull服务开发的新手,我正在寻找一些有用的信息以及有关使用webHttpBinding的经验反馈,与新的WCF Web API http://wcf.codeplex.com/相比.
我正在寻找的是了解webHttpBinding的缺点,以及为什么要使用新的Web api,特别是新API解决的问题.如果你能指点我一些博客文章比较他们或只是谈论使用webHttpBinding时的问题我会很感激.先感谢您.
我正在使用NuGet的HttpClient 0.6.0.
我有以下C#代码:
var client = new HttpClient(new WebRequestHandler() {
CachePolicy =
new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable)
});
client.GetAsync("http://myservice/asdf");
Run Code Online (Sandbox Code Playgroud)
该服务(这次是CouchDB)返回ETag值和状态代码200 OK.返回一个Cache-Control标头,其值为must-revalidate
更新,这是来自couchdb的响应头(取自visual studio调试器):
Server: CouchDB/1.1.1 (Erlang OTP/R14B04)
Etag: "1-27964df653cea4316d0acbab10fd9c04"
Date: Fri, 09 Dec 2011 11:56:07 GMT
Cache-Control: must-revalidate
Run Code Online (Sandbox Code Playgroud)
下次我执行完全相同的请求时,HttpClient会执行条件请求并返回304 Not Modified.哪个是对的.
但是,如果我使用具有相同CachePolicy的低级HttpWebRequest类,则第二次甚至不会发出请求.这就是我希望HttpClient也表现的方式.
它是必须重新验证的标头值还是为什么HttpClient表现不同?我想只做一个请求,然后在没有条件请求的情况下从缓存中获取其余的请求.
(另外,作为附注,在调试时,响应状态代码显示为200 OK,即使服务返回304 Not Modified)
在名为Payment的API控制器中,我有以下方法:
[HttpPost]
public HttpResponseMessage Charge(Payment payment)
{
var processedPayment = _paymentProcessor.Charge(payment);
var response = Request.CreateResponse(processedPayment.Status != "PAID" ? HttpStatusCode.ExpectationFailed : HttpStatusCode.OK, processedPayment);
return response;
}
Run Code Online (Sandbox Code Playgroud)
在我的HTML页面中,我有:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:65396/api/payment/charge",
data: $('#addPayment').serialize(),
dataType: "json",
success: function (data) {
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud)
每当我解雇POST时,我都会
"NetworkError: 405 Method Not Allowed - http://localhost:65396/api/payment/charge"
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢.
UPDATE
这是路由信息(默认)
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用VS 2012 RC,我正在创建一个ASP.NET MVC 4 Web应用程序,我计划在其中提供基于HTML的用户界面和基于WebApi的编程接口.
对于我的HTML网站,我有一个控制器和每个模型的视图(MVC!),并且路由"按惯例"工作,例如,URL /client连接到我的ClientController.我的ClientController来源于Controller.
对于我的API,我将创建源自的新控制器ApiController.我自然希望我的API网址与我的HTML网址类似,因此我希望客户端信息可用于/api/client.但是,使用按惯例路由,这表明我需要一个命名的ApiController ClientController.我已经ClientController上课了.
我该如何处理?我需要自定义路由吗?我是否将API类放在不同的命名空间中,以便我可以为它们指定相同的名称?
更新:这个问题似乎表明我的API控制器的不同命名空间就是我所需要的:混合web api控制器和站点控制器
我有一个我正在构建的应用程序,目前,我使用ASMX构建了一些Web服务.最后,应用程序将部署在azure上.Web服务非常简单,他们所做的只是在AppCode文件夹中调用一个处理所有工作的类.
将我的Web服务移动到WCF或Web API会更好/更容易/更快/更高效吗?
谢谢你的建议.
PS:我想补充一点,Web服务需要在HTTPS中工作.目前,他们在HTTP上,因为我处于开发模式.