我想尝试使用Web API进行休息调用,但我希望响应是存储在数据库中的实际二进制映像,而不是JSON base64编码的字符串.有人对此有一些指示吗?
更新 - 这是我最终实现的:
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new MemoryStream(profile.Avatar));
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "avatar.png";
return result;
Run Code Online (Sandbox Code Playgroud) 在MVC2中,我曾经以一种方式创建强类型视图,当我发布时,我从未使用过FormCollection对象.我的签名总是这样:
[AcceptVerbs(HttpVers.Post)]
public Create(Person newPerson)
{
//code to update the person from the post
}
Run Code Online (Sandbox Code Playgroud)
但是现在我看到了这个新的TryUpdateModel方式,我只想写下这样的东西:
[AcceptVerbs(HttpVers.Post)]
public Create()
{
Person thePersonToCreate = new Person()
TryUpdateModel(thePersonToCreate)
{
//Code to create the person if model is valid
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在看来我必须模拟HTTPContext才能测试这个方法.但是,似乎我仍然可以使用强类型方法的前一种方式.我意识到TryUpdateModel方法对那些使用FormCollection方法的人来说是一种改进,但为什么还要使用TryUpdateModel?
我正在为以下映射获得堆栈溢出:
Mapper.CreateMap<Parent, ParentViewModel>()
.ForMember(x => x.Children, o => o.MapFrom(x => x.Children.ConvertToChildrenViewModel()));
Mapper.CreateMap<Children, ChildrenViewModel>()
.ForMember(x => x.Parents, o => o.MapFrom(x => x.Parents.ConvertToParentViewModel()));
Run Code Online (Sandbox Code Playgroud)
我理解为什么会发生这种情况,这显然是一个无限循环.我怎么能让它在automapper中工作?我需要父母了解他们的孩子和他们的孩子,了解他们的父母.我将不得不创建另一个ViewModel
用于Children.Parents
不包含的Parents.Children
财产?
扩展方法示例,类似于儿童:
public static IList<ParentViewModel> ConvertToParentViewModel(this IEnumerable<Parent> parents)
{
return Mapper.Map<IList<ParentViewModel>>(parents);
}
Run Code Online (Sandbox Code Playgroud) 我正在我的MVC应用程序中创建ViewModels.我们使用域模型的automapper来查看模型转换.我的问题是我在MVC中执行ajax时遇到循环引用错误(好像是导致问题的JavaScriptSerializer),所以不需要返回项目列表,我只需要计数(因为这是我所有的视图模型需求) ).以下是层次结构的示例.提前感谢任何建议!
public class ProjectViewModel
{
public int ProjectID { get; set; }
[Required]
[UIHint("Project Name")]
public string Name { get; set; }
public ICollection<ProjectGroupViewModel> ProjectGroups { get; set; }
}
public class ProjectGroupViewModel
{
public int ProjectGroupID { get; set; }
[Required]
public string Name { get; set; }
//THIS is what I Want to have as int ProjectCount
public ICollection<ProjectViewModel> Projects { get; set; }
}
Run Code Online (Sandbox Code Playgroud)