尝试从httpcontext读取形式数据的错误
我有一个dotnet core Web项目,在其中添加了此JavaScript,
$trigger.on('click', function () {
const $button = $(this);
const resortId = $button.data('resortId');
$.post(url, { resortId: resortId }, function (response) {
etc...
});
});
ID正确传递并传递给以下控制器方法,
[HttpPost]
[Route("wishlist/add")]
public JsonResult Add()
{
var response= _repository.Add(Request, HttpContext.Session);
return Json(response);
}
该方法利用了这一点代码,
private static int GetId(HttpRequest request)
{
var form = request.Form;
int.TryParse(form["resortId"], out var resortId);
return resortId;
}
这似乎是代码失败的地方
var form = request.Form; (Line 82)
,对我来说很漂亮无害! 此代码在我的本地设置中非常有效,但是当它位于我们的登台服务器上时,它不起作用!
这是我尝试使用jQuery ajax调用而不是帖子发送请求的完整堆栈跟踪
------->
Message. "Incorrect Content-Type: "
Inner Exception.
""
StackTrace.
" at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()
at myproject.GetResortId(HttpRequest request) in Repository.cs:line 82
at Add(HttpRequest request, ISession session) in Repository.cs:line 31
at myproject.Controllers.Controller.Add() in Controller.cs:line 28
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)"
<-------
,以便我可以明确说明内容类型,但这无法解决问题。
我认为这并不重要,但是该服务器上没有SSL证书(我最初看到405响应,想知道这是否是原因)。
这是经典的“在我的机器上工作”的情况,但我不确定如何进一步调试此问题。
Update
我现在对此进行了重构以利用新对象
控制器,
[HttpPost]
[Route("wishlist/add")]
public JsonResult Add(WishListAddRequest request)
{
etc...
}
从而将其从会话中获取并将其传递给控制器的项目进行更新。
现在,我在舞台上从服务器获得了405响应,并且从本地计算机上可以很好地工作。
更新2
这是对我的特定问题的答案,尽管这对面对问题的其他人并不特别有用。
我将这些方法重构为HTML表格,并将其张贴到控制器中,以删除对JavaScript的需求。
我确实尝试在登台网站上添加SSL证书,以排除那里的任何问题,但遇到了与网络相关的问题。
我怀疑,由于该网站的规则将HTTP请求推向没有SSL的服务器上的HTTP,因此我的Ajax呼叫对CORS犯规,并且如果我能证明我会在问题上添加答案。
I have a dotnet core web project in which I have added this javascript
$trigger.on('click', function () {
const $button = $(this);
const resortId = $button.data('resortId');
$.post(url, { resortId: resortId }, function (response) {
etc...
});
});
The id is being passed correctly and is passed to the following controller method
[HttpPost]
[Route("wishlist/add")]
public JsonResult Add()
{
var response= _repository.Add(Request, HttpContext.Session);
return Json(response);
}
Which makes use of this bit of code
private static int GetId(HttpRequest request)
{
var form = request.Form;
int.TryParse(form["resortId"], out var resortId);
return resortId;
}
This seems to be where the code is failing
var form = request.Form; (Line 82)
Which to me is pretty innocuous!
This code works perfectly in my local setup, however when it sits on our staging server it doesn't work!
This is my full stack trace
------->
Message. "Incorrect Content-Type: "
Inner Exception.
""
StackTrace.
" at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()
at myproject.GetResortId(HttpRequest request) in Repository.cs:line 82
at Add(HttpRequest request, ISession session) in Repository.cs:line 31
at myproject.Controllers.Controller.Add() in Controller.cs:line 28
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)"
<-------
I have attempted to send the request using the jQuery ajax call instead of post so that I can explicitly state the content type, but this does not fix the issue.
I don't think it's important but there isn't an SSL certificate on this server (I was originally seeing a 405 response and wondered if this was why).
It's a classic "Works on my machine" situation, but I'm not sure how to further debug this.
Update
I have now refactored this to make use of a new object
Controller
[HttpPost]
[Route("wishlist/add")]
public JsonResult Add(WishListAddRequest request)
{
etc...
}
This gets the collection from the session and updates it with the item passed to the controller.
I now get a 405 response from the server on Staging and it works absolutely fine from my local machine.
Update 2
This serves as an answer to my particular problem although it isn't especially helpful to anyone else facing the issue.
I refactored the methods into html forms and posted these to the controllers removing the need for the JavaScript.
I did try to add an SSL certificate to the Staging website to rule out any issues there but ran into network-related problems.
I suspect that because the website has redirect rules pushing http requests to https on a server without SSL, my Ajax calls were falling foul of CORS and if I could prove that I would add an answer to the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论