我正在尝试获取一些api用户请求信息,例如他们使用的设备和操作系统,所以我尝试了这样:
private string GetDeviceInfo()
{
var userAgent = HttpContext.Current.Request.UserAgent;
var uaParser = Parser.GetDefault();
var c = uaParser.Parse(userAgent);
return c.Device + "|" + c.OS + "|" + c.UserAgent;
}
Run Code Online (Sandbox Code Playgroud)
但HttpContext.Current.Request.UserAgent总是空的!我搜索了一下并尝试了这个链接,你能告诉我有什么问题吗?
我有一个async功能.有多个级别的函数调用都返回a Task.在这个链中的某个点上,我必须修改结果的值.我想这样做而不会中断async函数的流程.注意:
[HttpGet]
[Route("GetWeb")]
public async Task<IHttpActionResult> GetResult([FromUri] string url)
{
var result = await GetPageText(url);
return Ok(result);
}
private Task<string> GetPageText(string url)
{
Task<string> data = GetDataAsync(url);
//here I would like to manipulate the data variable
return data;
}
Run Code Online (Sandbox Code Playgroud)
在该函数中,GetPageText如何在不中断异步流的情况下操作数据变量.这可能吗?
我有一个ApiController与以下方法:
public void Post(dynamic data)
{
Type actualType = data.GetType(); // returns JObject
}
Run Code Online (Sandbox Code Playgroud)
当我进行AJAX调用,将JSON数据发送到控制器时,'data'参数的实际类型是JObject(来自Newtonsoft.Json库).WebAPI的哪个部分负责将此请求中的数据绑定到JObject?它是模型粘合剂吗?如果是这样,哪一个?(有几个内置的,例如'MutableObjectModelBinder','TypeMatchModelBinder'等)
在我的web api应用程序中,我接受Application/Json mediatype作为请求.因此,我希望自定义帮助页面以删除其他格式,例如application/xml,text/xml,application/x-www-form-urlencoded等.任何帮助都非常明显.
使用web api时,如果使用,如何调用正确的路由方法 [RoutePrefix()]
假设您有类似"MyReallyLongNamedClassController"的内容.默认路由为http:... com/api/MyReallyLongNamedClass.然后应用程序通过名为Get,Post,Put等的方法(除非当然使用动词装饰器).
如果我[RoutePrefix("api/LongClass")]在我的控制器上放置一个路由前缀装饰器,我怎么能让web api仍然使用这些方法的默认值?
意思是,我希望名为"GetAll()"的方法仍然映射到"api/LongClass"(当使用get头时)和"PostThis(int id)"仍然映射到"api/LongClass/{id}"(当使用帖子标题时)
我有一个简单的类TestController,它派生自ApiController并处理POSTHttp请求。我也有一个监听的HttpSelfHostServer实例http://localhost:12357
using System;
using System.Net.Http;
using System.ServiceModel;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace RestController
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:12357");
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
public class TestController : ApiController
{
[HttpPost]
public HttpResponseMessage PostMethodFactory()
{
return new HttpResponseMessage();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何连接TestController到HttpSelfHostServer。如何提出所有路由到班级的POST请求?http://localhost:12357TestController
我有一个角度控制器,它是一个Web API方法的帖子.我试图在ienumerable上添加null安全性,但是当我这样做时,导致ienumerable总是为空.
角度呼叫
$http.post(customer/DoSomething, {names: ["chris", "joe"]})
Run Code Online (Sandbox Code Playgroud)
方法
string DoSomething(Customer customer){...}
Run Code Online (Sandbox Code Playgroud)
模型
// populates IEnumerable
public class Customer
{
public IEnumerable<string> Names {get; set;}
}
// what I want to do but IEnumerable is always empty
public class Customer
{
private IEnumerable<string> _names;
public IEnumerable<string> Names
{
get
{
return _names ?? new List<string>();
}
set
{
_names = value;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在关注一些在线示例,由于某种原因,我无法成功反序列化以下XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<postcode>BD1 1LT</postcode>
<geo>
<lat>53.793063240118215</lat>
<lng>-1.7540318423563699</lng>
<easting>416300.0</easting>
<northing>433000.0</northing>
<geohash>http://geohash.org/gcwf00dz21g1</geohash>
</geo>
<administrative>
<council>
<title>Bradford</title>
<uri>http://statistics.data.gov.uk/id/statistical-geography/E08000032</uri>
<code>E08000032</code>
</council>
<ward>
<title>City</title>
<uri>http://statistics.data.gov.uk/id/statistical-geography/E05001347</uri>
<code>E05001347</code>
</ward>
<constituency>
<title>Bradford West</title>
<uri>http://statistics.data.gov.uk/id/statistical-geography/E14000589</uri>
<code>E14000589</code>
</constituency>
</administrative>
</result>
Run Code Online (Sandbox Code Playgroud)
我尝试过各种组合:
[DataContract(Name = "result", Namespace = "")]
public class Result
{
[DataMember(Name = "postcode")]
public string Postcode { get; set; }
[DataMember(Name = "geo")]
public Geo Geo { get; set; }
}
[DataContract(Name = "geo")]
public class Geo
{
[DataMember(Name = "lat")]
public string Lat { get; …Run Code Online (Sandbox Code Playgroud) 我过滤掉了用户在webapi Get call中可以看到的文档.Document对象中的CompanyName必须与分配了CompanyName的用户匹配.因此,如果分配了CompanyName1和CompanyName2的用户只能返回与其匹配的文档.
public List<Document> GetDocuments()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = manager.FindById(User.Identity.GetUserId());
using (var context = new ApplicationDbContext())
{
return context.Documents
.Where(j => j.CompanyName == user.CompanyName1) || (j => j.CompanyName == user.CompanyName2) || (j => j.CompanyName == user.CompanyName2)
.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,但这是我需要它做的.
错误1运算符'||' 不能应用于'System.Linq.IQueryable'和'lambda expression'类型的操作数
错误2运营商'.' 不能应用于'lambda表达式'类型的操作数
我有一个具有以下拓扑的服务.
A(Web应用程序)---调用---> B(本地Web Api)---调用---> C(远程Web Api)
我试图捕获Fiddler中从B到C的流量,但没有记录任何内容.
B是IIS中我的开发箱本地托管的网站.它在具有除我的凭据之外的凭据的应用程序池上运行,因为它具有不同的权限集.即使在我完成以下重定向后,从B发出的流量也没有被记录:
<system.net>
<defaultProxy
enabled = "true"
useDefaultCredentials = "true">
<proxy autoDetect="false" bypassonlocal="false" proxyaddress="http://127.0.0.1:8888" usesystemdefault="false" />
</defaultProxy>
Run Code Online (Sandbox Code Playgroud)
如何捕获离开B的流量?
asp.net-web-api ×10
c# ×9
asp.net ×4
.net ×3
angularjs ×1
asp.net-mvc ×1
async-await ×1
asynchronous ×1
fiddler ×1
iis ×1
json.net ×1
lambda ×1
routing ×1
xml ×1