asp.net mv4 controller 跳转 静态html,c# - Disposed object error when dealing reading multipart form data...

The setup I have is an asp.net mvc 4 project using .net 4.5 and Entity Framework 5. The language is C#.

The code is mainly a straight copy from the microsoft tutorials with using multiform data to handle the post requests. What is happening is that when I attempt to access the ContentLength of the file, it throws a System.ObjectDisposedException. The full stacktrace follows the code snippet.

public async Task 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);

try

{

// Read the form data.

await Request.Content.ReadAsMultipartAsync(provider);

// This illustrates how to get the file names.

foreach (MultipartFileData file in provider.FileData)

{

Debug.WriteLine(file.Headers.ContentDisposition.FileName);

Debug.WriteLine("Server file path: " + file.LocalFileName);

Debug.WriteLine("Content type: " + file.Headers.ContentType);

Debug.WriteLine("This wont work: " + file.Headers.ContentLength);

}

return Request.CreateResponse(HttpStatusCode.OK, "OK");

}

catch (System.Exception e)

{

return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);

}

}

Stacktrace:

A first chance exception of type 'System.ObjectDisposedException' occurred in System.Net.Http.dll

iisexpress.exe Error: 0 : Operation=ReflectedHttpActionDescriptor.ExecuteAsync, Exception=System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Http.StringContent'.

at System.Net.Http.HttpContent.CheckDisposed()

at System.Net.Http.HttpContent.GetComputedOrBufferLength()

at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()

at FieldAssistant.Controllers.SpeedTestController.d__0.MoveNext() in c:\Users\matt.delves\blah\FieldAssistant\Controllers\SpeedTestController.cs:line 48

iisexpress.exe Error: 0 : Operation=ApiControllerActionInvoker.InvokeActionAsync, Exception=System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Http.StringContent'.

at System.Net.Http.HttpContent.CheckDisposed()

at System.Net.Http.HttpContent.GetComputedOrBufferLength()

at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()

at FieldAssistant.Controllers.SpeedTestController.d__0.MoveNext() in c:\Users\matt.delves\blah\FieldAssistant\Controllers\SpeedTestController.cs:line 48

iisexpress.exe Error: 0 : Operation=SpeedTestController.ExecuteAsync, Exception=System.ObjectDisposedException: Cannot access a disposed object.

Object name: 'System.Net.Http.StringContent'.

at System.Net.Http.HttpContent.CheckDisposed()

at System.Net.Http.HttpContent.GetComputedOrBufferLength()

at System.Net.Http.Headers.HttpContentHeaders.get_ContentLength()

at FieldAssistant.Controllers.SpeedTestController.d__0.MoveNext() in c:\Users\matt.delves\blah\FieldAssistant\Controllers\SpeedTestController.cs:line 48

iisexpress.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance

iisexpress.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate

iisexpress.exe Information: 0 : Response, Status=500 (InternalServerError), Method=POST, Url=http://192.168.2.63:51230/api/speedtest, Message='Content-type='application/json; charset=utf-8', content-length=unknown'

iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync

iisexpress.exe Information: 0 : Operation=SpeedTestController.Dispose

The program '[4436] iisexpress.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

There have been some suggestions that modifying the web.config with a couple of lines are meant to resolve the issue but having inserted those, I found that they didn't work.

I'm not primarily a microsoft / c# guy so as much explanation in the response would be greatly appreciated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值