【问题标题】:Routing from View to a Razor Page action Asp.Net Core 3从视图路由到 Razor 页面操作 Asp.Net Core 3
【发布时间】:2020-02-25 17:17:57
【问题描述】:

我有一个正在由 MVC 处理的 UnderConstruction 页面。在将用户的电子邮件地址输入到注册页面后,该视图具有以下代码来路由用户,该注册页面是身份支架剃刀区域的一部分。目的是打开注册页面,并在电子邮件输入中填充他们的电子邮件。

<!-- route to Register Form -->
<form class="form-inline push-10"   method="get">
   <div class="form-group">
      <label class="sr-only text-white" asp-for="Email">Email Address</label>
      <input class="form-control" type="email" asp-for="Email" placeholder="Your Email..">
  </div>
  <div class="form-group">
     <button class="btn btn-default" type="submit" asp-area="Identity" asp-page="/Account/Register" asp-page-handler="WithEmail">
       <i class="fa fa-plus"></i> Register
    </button>
  </div>
  <div class="form-group">
     <a class="btn btn-default" asp-controller="Home" asp-action="Index">
        <i class="fa fa-home"></i> Home
     </a>
  </div>

和页面视图模型

 public class UnderConstructionViewModel
{
    public string Email { get; set; }
}

输入电子邮件地址后单击“注册”按钮会生成此 URL

https://localhost:44367/Identity/Account/Register?Email=testemail@mydomain.com

这会将您带到注册页面,其中未在电子邮件输入中填充电子邮件地址。

这里是register.cshtml.cs中的方法(用于标识的脚手架剃刀区域)

public IActionResult OnGetWithEmail(string email)
{
   return RedirectToPage("Register", new InputModel{ Email = email });
}

问题是,该方法永远不会被击中。处理程序遵循约定,模型遵循约定,所以我不确定为什么该方法没有被命中。

我也尝试了表单调用中的 asptag 助手,但产生了相同的结果。

如有任何帮助解决此问题,我们将不胜感激。

【问题讨论】:

    标签: c# asp.net-mvc razor-pages asp.net-core-3.0


    【解决方案1】:

    任何附加到表单action 属性的查询字符串值,或者在本例中为formaction 属性,在提交表单时都会被路由系统清除。这似乎是设计使然:https://github.com/aspnet/Mvc/issues/8621

    您可以通过将"{handler?}" 添加到注册页面中的@page 指令来解决此问题。然后处理程序名称作为段而不是查询字符串值附加到 URL。

    【讨论】:

      猜你喜欢
      • 2019-05-22
      • 2020-05-06
      • 2018-01-31
      • 2018-02-24
      • 1970-01-01
      • 2020-05-06
      • 2018-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多