按照ASP.NET上的教程,实现了一个Web API控制器方法,用于执行如下所示的异步文件上载:
public Task<HttpResponseMessage> PostFormData()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
return Request.CreateResponse(HttpStatusCode.OK);
});
return task;
}
Run Code Online (Sandbox Code Playgroud)
通过标准的多部分HTML表单上传文件非常有效.但是,当另一个开发人员尝试通过Flex的FileReference类构造的多部分表单上载文件时,会抛出错误:
MIME多部分流的意外结束.MIME多部分消息未完成.
我不知道问题出在Web API或Flex上.我找到了一些没有任何影响的相关修复程序(使用ASP.Net Web API的多部分表单POST),最近发现了这一个("多部分流.MIME多部分消息未完成"webapi上传错误).如果第二个链接成立,是否有人知道它是否在通过Nuget提供的当前版本的Web API中出现?讨论是在5月份,Nuget的最新版本是8月,所以我认为这个修补程序已经部署,并不是我的问题的根本原因.
我正在尝试检索父实体的所有集合(非笛卡儿产品),但无法弄清楚如何获得孙子.表结构如下:

下面的代码获取了我的父代及其Child1和Child2集合,以及它的ParentChild3集合,但我不知道如何构造查询以获取Child3孙子(并将它们推迟到Future()).
var parent = _session
.QueryOver<Parent>()
.Where(x => x.Id == id)
.Future<User>();
var children1 =_session
.QueryOver<Parent>()
.Where(x => x.Id == id)
.Fetch(x => x.Children1).Eager
.Future<Parent>();
var children2 =_session
.QueryOver<Parent>()
.Where(x => x.Id == id)
.Fetch(x => x.Children2).Eager
.Future<Parent>();
var parentChildren3 =_session
.QueryOver<Parent>()
.Where(x => x.Id == id)
.Fetch(x => x.ParentChildren3).Eager
.Future<Parent>();
// how to get children3, now?
return parent.SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
半相关:这是获得所有收藏品的最佳方式吗?是否更好(并且可能)使用通过连接获得结果的查询?
我试图使用JoinAlias加入多个实体,并且无法弄清楚如何让多个实体加入.以下代码导致SQL错误:
[TestMethod]
public void TestAliases()
{
App_Start.NHibernateProfilerBootstrapper.PreStart();
var type = new ShirtStyleType {Id = 1};
var style = new ShirtStyle {Id = 1, ShirtStyleType = type};
var shirt = new Shirt {Id = 1};
var shirtStyle = new ShirtShirtStyle {Shirt = shirt, ShirtStyle = style};
shirt.ShirtStyles = new[] {shirtStyle};
using (Session.BeginTransaction())
Session.Save(shirt);
Session.Clear();
using (Session.BeginTransaction())
{
Shirt shirtAlias = null;
ShirtShirtStyle shirtStylesAlias = null;
ShirtStyle shirtStyleAlias = null;
ShirtStyleType shirtStyleTypeAlias = null;
var query = Session.QueryOver<Shirt>(() => shirtAlias)
.JoinAlias(() => …Run Code Online (Sandbox Code Playgroud) 测试用于文件上载的Web API,有一个像这样的简单视图模型:
public class TestModel {
public string UserId {get;set;}
public HttpPostedFileBase ImageFile {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
用于方法:
[HttpPost]
public void Create(TestModel model)
Run Code Online (Sandbox Code Playgroud)
当我尝试将多部分/表单数据编码的表单发布到操作时,我收到此异常:
System.InvalidOperationException: No MediaTypeFormatter is available to read an object of type 'TestModel' from content with media type 'multipart/form-data'.
at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at …Run Code Online (Sandbox Code Playgroud) 开始编写一个简单的过滤器,在每个动作加载时从请求中提取一些东西,从其他堆栈流中复制一些代码,如下所示:
public class TestKeyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
if (context.Request.Properties.ContainsKey("test"))
{
// do stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后添加其余的属性:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
GlobalConfiguration.Configuration.Filters.Add(new ElmahHandledErrorLoggerFilter());
filters.Add(new HandleErrorAttribute());
filters.Add(new TestKeyAttribute());
}
Run Code Online (Sandbox Code Playgroud)
在运行时,会导致此错误:
The given filter instance must implement one or more of the following filter
interfaces: IAuthorizationFilter, IActionFilter, IResultFilter, IExceptionFilter.
Run Code Online (Sandbox Code Playgroud)
我发现的大多数链接都与MVC 3有关,这似乎有效; 然而,我正在使用MVC 4并使用Web API - 我现在需要注册属性吗?
请注意:我不希望附加到Web API控制器的过滤器(将其添加到GlobalConfiguration.Configuration.Filters确实有效),而是普通的Web控制器.
编辑:我知道我可以通过继承IActionFilter而使用OnActionExecuting来实现这一点,我只是好奇为什么这种方法不起作用,因为一堆教程似乎应该说.
在web.config中存储连接字符串是最安全的解决方案吗?有没有办法将它们存储在部署它们的服务器上(例如,作为系统DSN,虽然我认为这些很麻烦)?
我正在将我的工作代码库移到BitBucket,由于某种原因,我非常不满意在我没有严格控制权的机器上使用包含数据库用户名/密码的web.config的想法.我可能只是表现得非理性; 从未在外部托管任何代码.
尝试使用NHIDnate的LINQ提供程序编写动态查询,但我遇到了问题.我的理解是LINQ查询被推迟到调用之前(即使用ToList()),所以我有以下代码:
string[] filteredIds = new[] { "someIdNotInUse"};
var result = _products
.GetAll()
.Skip(0)
.Take(10);
if (filteredIds != null)
{
result.Where(x => x.Child1.Child2.Any(z => filteredIds.Contains(z.Child3.Id)));
}
var r = result.ToList();
Run Code Online (Sandbox Code Playgroud)
不应用条件块中的Where过滤器; 当我运行.ToList时,我得到的记录我没想到.但是,如果我删除where过滤器并将其直接附加到_products调用,它将按预期工作.我误解了LINQ提供程序的工作原理吗?如何创建这样的查询,而无需为每个可能的过滤条件和组合重写查询?