我有一个方法,如下所示进行POST
var response = await client.PostAsJsonAsync(url, entity);
if (response.IsSuccessStatusCode)
{
// read the response as strongly typed object
return await response.Content.ReadAsAsync<T>();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何获得从实体对象发布的实际JSON.我想记录获得POSTED的JSON,所以如果没有我必须自己进行json序列化,那将是很好的.
使用反射,我试图找到从给定基类继承的类型集.花了很长时间才弄清楚简单的类型,但是当涉及到泛型时我很难过.
对于这段代码,第一个IsAssignableFrom返回true,但第二个返回false.然而,最后的任务编译得很好.
class class1 { }
class class2 : class1 { }
class generic1<T> { }
class generic2<T> : generic1<T> { }
class Program
{
static void Main(string[] args)
{
Type c1 = typeof(class1);
Type c2 = typeof(class2);
Console.WriteLine("c1.IsAssignableFrom(c2): {0}", c1.IsAssignableFrom(c2));
Type g1 = typeof(generic1<>);
Type g2 = typeof(generic2<>);
Console.WriteLine("g1.IsAssignableFrom(g2): {0}", g1.IsAssignableFrom(g2));
generic1<class1> cc = new generic2<class1>();
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何在运行时确定一个泛型类型定义是否来自另一个?
我创建了一个mvc站点,我将大量的json表单数据(Content-Type:application/x-www-form-urlencoded)发送回mvc控制器.当我这样做时,我收到500响应,声明:"InvalidDataException:超出表单值计数限制1024."
在以前版本的aspnet中,您可以将以下内容添加到web.config以增加限制:
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="5000" />
<add key="aspnet:MaxJsonDeserializerMembers" value="5000" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
当我将这些值放在web.config中时,我没有看到任何更改,所以我猜测Microsoft不再从web.config中读取这些值.但是,我无法弄清楚应该在哪里设置这些设置.
任何有关增加表单值计数的帮助都非常感谢!
需要说明的是,当我的帖子数据中的项目数小于1024时,此请求可以正常工作.
我有一个模型类,我想在我的ASP.NET MVC Core(RC2)应用程序中绑定查询字符串.
我需要在查询字符串键中支持下划线以确认OAuth规范,但我想在我的应用程序中使用标题案例属性名称.
我的模型类看起来像这样:
class OauthParameters
{
public string ClientId {get; set;}
public string ResponseType {get; set;}
public string RedirectUri {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
所以我想绑定查询字符串像client_id,response_type并redirect_uri给它.
有没有一种方法可以让ASP.NET MVC Core自动或通过属性注释来实现?
我已经阅读了一些关于编写自定义模型绑定器的文章,但是这些似乎(1)对于我想要实现的内容而言过于复杂,并且(2)是为RC1或更早版本编写的,并且一些语法已经改变.
提前致谢.
我正在尝试使用Reflection并遇到以下情况.
在下面的代码中,我们假设'obj'可以是类型IEnumerable<>或ICollection<>或IList<>.
我想将此System.Object强制转换为IEnumerable<>(始终如此)ICollection<>并IList<>继承IEnumerable<>,以便我想枚举集合并使用反射来编写单个项目.
这背后的动机是我只是想看看Serializers如何序列化数据,所以我试图模拟这种情况,希望也能理解Reflection.
我想把对象转换为非通用的IEnumerable,但是认为这会导致不必要的对象装箱,当我们说...的实际情况时IEnumerable<int>......我在想什么?
private void WriteGenericCollection(object obj)
{
Type innerType = obj.GetType().GetGenericArguments()[0];
//Example: IEnumerable<int> or IEnumerable<Customer>
Type generatedType = typeof(IEnumerable<>).MakeGenericType(innerType);
//how could i enumerate over the individual items?
}
Run Code Online (Sandbox Code Playgroud) 使用新的webapi 2.1位.出于某种原因,当我打开帮助页面时,没有任何属性的描述细节,例如
/// <summary>
/// Some summary that shows correct
/// </summary>
[DataContract]
public class MyClass
{
/// <summary>
/// Display something.....
/// </summary>
[DataMember(Name = "Great")]
public string MyGreatProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我打开帮助页面时,我可以看到摘要"显示正确的一些摘要"但是对于此模型的属性,没有任何摘要显示.
webapi 2不支持属性描述吗?
我正在尝试为我的.Net Web API应用程序中的某些域启用CORS,并且能够通过此代码在Application Start上执行此操作.
public static void Register(HttpConfiguration config)
{
//below comma separated string is built from database
var domains = "http://www.myfirstdomain.com,http://www.myseconddomain.co.uk .... about 130 domains more..";
config.EnableCors(new EnableCorsAttribute(domains, "*", "*"));
Run Code Online (Sandbox Code Playgroud)
但是,如果在应用程序处于活动状态时添加了新域,则在回收应用程序池并再次构建此列表之前,将不允许这些域发布.
有没有什么办法可以在我的应用程序生命周期内更新此列表?我知道我可以定期回收应用程序池,但这会导致某些请求出现延迟,理想情况下我可以不这样做.
我知道我可以在控制器方法上启用它,即..
[EnableCors("http://domain1.com,http://domain2.com", "*", "*")]
public HttpResponseMessage PostAsync([FromBody] MyRequest myRequest)
{
Run Code Online (Sandbox Code Playgroud)
但是,逗号分隔参数必须再次声明为常量,因此不能是动态的.
我是否遗漏了一些明显的显而易见的事情,或者是否有人会想到这样做的好方法?
编辑
这是我尝试编写自己的自定义EnableCors属性.
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class EnableCorsByDomainAttribute : Attribute, ICorsPolicyProvider
{
private readonly CorsPolicy _policy;
public EnableCorsByDomainAttribute()
{
_policy = new CorsPolicy
{
AllowAnyMethod = true,
AllowAnyHeader = true
};
var …Run Code Online (Sandbox Code Playgroud) 我想create.sql在我的服务器的主文件夹中访问我的文件.它包含用于设置数据库的查询.我有一个问题,根本无法访问此文件.
1)我无法真正实现目标Configuration.我只能用AddJsonFile,AddXmlFile和AddIniFile.我想这不是一个把大sql文件放到其中任何一个的最好的主意.
2)github上的Mvc源似乎丢失了MapPath.所以不可能使用Server.MapPath("~/create.sql").
那么如何实现呢?
我正在尝试使用N-UNIT来测试我的Web API应用程序,但我无法找到一种正确的方法来测试我的文件上传方法.哪种方法是测试方法的最佳方法?
Web API控制器:
[AcceptVerbs("post")]
public async Task<HttpResponseMessage> Validate()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
return Request.CreateErrorResponse(HttpStatusCode.UnsupportedMediaType,"please submit a valid request");
}
var provider = new MultipartMemoryStreamProvider(); // this loads the file into memory for later on processing
try
{
await Request.Content.ReadAsMultipartAsync(provider);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
foreach (var item in provider.Contents)
{
if (item.Headers.ContentDisposition.FileName != null)
{
Stream stream = item.ReadAsStreamAsync().Result;
// do some stuff and return response
resp.Content = new StringContent(result, Encoding.UTF8, "application/xml"); …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 ASP rest API 中实现 HATEOAS,将ReferenceResolverProvider.
问题是,根据我使用的控制器,我想使用不同的ReferenceResolvers,因为我需要对每个控制器采取不同的行为。
现在我有通用选项:
services.AddMvc()
.AddJsonOptions(option => option.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver())
.AddJsonOptions(options => options.SerializerSettings.ReferenceResolverProvider = () => new RoomsReferenceResolver<Room>())
.AddJsonOptions(options => options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects);
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
services.AddMvc()
.AddJsonOptions(option => option.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver())
.AddJsonOptions<RoomsController>(options => options.SerializerSettings.ReferenceResolverProvider = () => new RoomsReferenceResolver<Room>())
.AddJsonOptions(options => options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects);
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×3
asp.net ×3
asp.net-core ×3
asp.net-mvc ×2
reflection ×2
ajax ×1
appsettings ×1
cors ×1
generics ×1
hateoas ×1
http ×1
http-post ×1
nunit ×1
types ×1
unit-testing ×1