c# - 如何“覆盖”AccountController 的 Index()?
问题描述
您可能已经知道,AccountController 自 2.0 起不再可用。多亏了 New Scaffolded Item,一切都可以通过 Identity.Pages 进行管理。
但我的问题是,如何从 url 中检索参数?
在 2.0 中,我们会这样做:
[Authorize]
public class AccountController : Controller
{
[HttpGet]
public ActionResult Index(string parameter)
{
[...]
}
}
但是在 3.1 中我们怎么做呢,因为 AccountController 已经不存在了?
解决方案
我假设您同时使用 AccountController 和 Scaffold Identity,并希望导航到 AccountController 中的操作方法并传输参数。
如果是这种情况,您可以参考以下示例代码:
在url末尾添加参数:
查看页面中的代码:
<a href="~/Account/Login?name='aaa'" >Login</a>
登录操作方法中的代码:
public class AccountController : Controller { public IActionResult Index() { return View(); } [HttpGet] public IActionResult Login(string name) { ViewData["name"] = name; return View(); } }
使用路由属性
查看页面中的代码:
<a class="nav-link text-dark" asp-area="" asp-controller="Account" asp-action="Login" asp-route-name="dillion">Account</a>
或者
<a href="~/Account/Login/aaa >Login</a>
登录操作方法中的代码:
public class AccountController : Controller { public IActionResult Index() { return View(); } [HttpGet] [Route("Account/Login/{name}")] public IActionResult Login(string name) { ViewData["name"] = name; return View(); } }
对于 Scaffold Identity 页面,如果您检查 url,您可以看到它们在 Identity 区域中,链接如下:
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
参考:
推荐阅读
- android - 空对象引用上的错误“java.lang.Class java.lang.Object.getClass()”
- javascript - jQuery在页面卸载时打破每个循环
- ruby-on-rails - 为什么 Rails 中的 SSE 挂在连接上?
- javascript - TypeScript 和 Redux:为什么我需要添加 ` | undefined` 到我的 Reducer 状态类型?
- npm - npm install 在 node_modules 的父文件夹中下载额外的依赖项
- c# - 视频转换:从 UDP 套接字到 Mat 对象的 h.264
- vb6 - “模板”字符串中的嵌套替换
- apache-kafka - Kafka:每个 poll() 调用是否一次只使用一个主题?
- c++ - 尝试编译简单的测试程序时 CMake 失败
- django - 如何在表单中检索 id 作为隐藏值,以便将该值作为外键存储在 django 的数据库中