如何用RazorPages路由替换MVC HomeController/Index重定向? [英] How to replace MVC HomeController/Index redirects with RazorPages routings?
本文介绍了如何用RazorPages路由替换MVC HomeController/Index重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从ASP MVC Classic迁移到ASP Razor Pages.
I'm migrating from ASP MVC Classic to ASP Razor Pages.
仅剩一个控制器可以迁移":HomeController
Only one controller left to "migrate": HomeController
public class HomeController : Controller
{
UserManager<WebUser> _userManager;
public HomeController(UserManager<WebUser> _userManager)
{
this._userManager = _userManager;
}
[Authorize]
public async Task<IActionResult> Index()
{
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
return RedirectToPage("/Account/Login", new { area = "WebUserIdentity" });
}
return RedirectToPage("/Index", new { area = "Downloads" });
}
}
没有与该控制器/动作对应的视图.
There is no corresponded view to this controller/action.
因此,我陷入了困境:如何配置剃须刀页面的路由以使用这些重定向(到两个不同区域)而不创建伪造"索引页面?
And because of this I'm in stuck: how to configure routing for razor pages to use those redirects (to two different areas) without creating "fake" Index page?
推荐答案
我相信您可以转换控制器以为索引页Pages/IndexModel
创建Page模型,并执行相同的重定向.
I believe you can convert the controller to create a Page model for index page Pages/IndexModel
and do the same redirects.
public class IndexModel : PageModel {
UserManager<WebUser> _userManager;
public IndexModel(UserManager<WebUser> _userManager) {
this._userManager = _userManager;
}
public async Task<IActionResult> OnGetAsync() {
var user = await _userManager.GetUserAsync(User);
if (user == null) {
return RedirectToPage("/Account/Login", new { area = "WebUserIdentity" });
}
return RedirectToPage("/Index", new { area = "Downloads" });
}
}
这篇关于如何用RazorPages路由替换MVC HomeController/Index重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文