Jay*_*aya 3 asp.net odata c#-4.0 asp.net-web-api2
我试图通过遵循本文来为odata服务实现客户端
我有的挑战:
按照文章中的步骤,我发现
1)(ProductClient).odata.config不是自动生成的-尽管我们可以创建一个
2)需要凭据才能访问端点的客户端(在我的情况下,这是基本身份验证)
3)最重要-找不到有关stackoverflow的相关文章:)
在下面为我这样的新手发布了解决方案!
解
为了实现类似于文章(即)中提到的内容
通过需要认证的Odata端点访问强类型的Odata实体-
static void ReadingODataEndPointByPassingMyBasicAuthCreds() {
// e.g. URL = http://localhost/myApi/odata
var url = ConfigurationManager.AppSettings["MyAPIBaseUrl"];
var container = new MyApi.Container(new Uri(url));
container.SendingRequest2 += SendBaseAuthCredsOnTheRequest;
foreach(var myEntity in container.MyEntities) {
Console.WriteLine(myEntity.Name);
Console.Write(string.Format("Description: {0}", myEntity.Description));
}
Console.Read();
}
private static void SendBaseAuthCredsOnTheRequest(object sender,
System.Data.Services.Client.SendingRequest2EventArgs e) {
var authHeaderValue = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", ConfigurationManager.AppSettings["username"]
, ConfigurationManager.AppSettings["password"])));
//this is where you pass the creds.
e.RequestMessage.SetHeader("Authorization", "Basic " + authHeaderValue);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5739 次 |
最近记录: |