为什么这段代码会从Request.Form中提供无效的内容类型? (ASP.NET核心)

时间:2017-08-28 02:51:51

标签: c# asp.net razor asp.net-core

这是使用RazorPages的ASP.NET Core 2.0 OnGet方法。

cs文件:

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CoreRazor2.Pages
{
  public class IndexModel : PageModel
  {
    [BindProperty]
    public int result { get; set; }
    public void OnGet(string operationType)
    {
      string result;
      switch (operationType)
      {
        case "+":
          result = Request.Form["First"];
          break;
        case "-":
          result = "1";
          break;
        case "/":
          result = "2";
          break;
        case "*":
          result = "3";
          break;
      }

    }
  }
}

cshtml文件:

@page
@model IndexModel
@{
    ViewData["Title"] = "Calculator";
}

<form method="GET">
<label>First Value: </label>
<input name="First"/>
<br/>
<br/>
<label>Second Value: </label>
<input name="second"/>
<br/>
<br/>
<input type="submit" name="operationType" value="+"/>
<input type="submit" name="operationType" value="-"/>
<input type="submit" name="operationType" value="*"/>
<input type="submit" name="operationType" value="/"/>
</form>
@Model.result

在第一个表单输入中输入值并单击“+”提交按钮时,程序会在Request.Form [“First”]处抛出以下异常:

Exception has occurred: CLR/System.InvalidOperationException
An exception of type 'System.InvalidOperationException' occurred in Microsoft.AspNetCore.Http.dll but was not handled in user code: 'Incorrect Content-Type: '
   at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
   at Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest.get_Form()
   at CoreRazor2.Pages.IndexModel.OnGet(String operationType) in c:\Users\Administrator\Desktop\CoreRazor2\Pages\Index.cshtml.cs:line 17
   at Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory.VoidHandlerMethod.Execute(Object receiver, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.<InvokeHandlerMethodAsync>d__29.MoveNext()

有没有人知道为什么或可以指出我一些有用的文件?

3 个答案:

答案 0 :(得分:5)

基于GET的表单通过URL而不是表单传递值。您需要使用Request.Query["First"]。 Request.Form仅在您发布表单时有效。但是,既然您正在使用Razor Pages,那么您可以省去所有麻烦并使用模型绑定:

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace CoreRazor2.Pages
{
    public class IndexModel : PageModel
    {
        public int Result { get; set; }

        public void OnGet(string operationType, int first, int second)
        {
            switch (operationType)
            {
                case "+":
                    Result = first + second;
                    break;
                case "-":
                    Result = first - second;
                    break;
                case "/":
                    Result = first / second;
                    break;
                case "*":
                    Result = first * second;
                    break;
            }

        }
    }
}

答案 1 :(得分:1)

我认为应该是

Request.Query["First"]

而不是

Request.QueryString["First"]

否则我得

  

无法将带有[]的索引应用于“QueryString”类型的表达式

答案 2 :(得分:0)

出于一般目的(如记录),当您需要处理所有类型的HttpRequest时,请使用HttpRequest.HasFormContentType

示例代码:

append