英文:
.net core Web Page not routing to inherited class with OnGet() method
问题
如何将它重定向到正确类中的 OnGet() 方法?
我已经实现了以下类似的代码,其中我有一个基本页面类和一个从基本页面继承的报告页面类。在运行时,启动路由应该加载 http://localhost:xxxx/Reporting。然而,报告类中的代码未被执行,包括 OnGet() 方法。相反,它尝试运行 BasePage 的 OnGet() 方法。
Shared\Components\BasePage\index.cshtml.cs
namespace Reporting.Web.Pages.Reports
{
[TypeFilter(typeof(RouteAccessAttribute), Arguments = new object[] { @"/BasePage/Index" })]
public class BasePageModel : PageModel
{
public readonly ILogger<BasePageModel> _logger;
public BasePageModel(
ILogger<BasePageModel> logger,
{
_logger = logger;
}
[BindProperty(SupportsGet = true)]
public bool GenerateReport { get; set; }
public ActionResult OnGet()
{
return RedirectToPage("/Status/Error");
}
}
}
Reporting\index.cshtml.cs
namespace Reporting.Web.Pages.Reports
{
[TypeFilter(typeof(RouteAccessAttribute), Arguments = new object[] { @"/Report/Index" })]
public class IndexModel : BasePageModel
{
public IndexModel(
ILogger<IndexModel> logger) : base(logger)
{
}
public async Task<IActionResult> OnGet()
{
var clientId = HttpContext.Session.Get<int>("ClientId");
if (clientId > 0)
{
Client = HttpContext.Session.Get<IEnumerable<Client>>("Clients").Where(x => x.Id.Equals(clientId)).FirstOrDefault();
return Page();
}
return RedirectToPage("/Status/Error");
}
}
}
此外,关于视图,我有相反的问题,它运行 Reporting\index.cshtml,但我希望它运行 BasePage\index.cshtml 下的通用代码。
英文:
How do I re-direct it to the OnGet() method in the correct class?
I have code implemented something like the below where I have a base page class and a reports page class that inherited from the base page. During runtime, the startup route is suppose to load http://localhost:xxxx/Reporting. However, the code in the reporting class is not executed including the OnGet() Method. Instead it tries to run the BasePage OnGet() method.
Shared\Components\BasePage\index.cshtml.cs
namespace Reporting.Web.Pages.Reports
{
[TypeFilter(typeof(RouteAccessAttribute), Arguments = new object[] { @"/BasePage/Index" })]
public class BasePageModel : PageModel
{
public readonly ILogger<BasePageModel> _logger;
public BasePageModel(
ILogger<BasePageModel> logger,
{
_logger = logger;
}
[BindProperty(SupportsGet = true)]
public bool GenerateReport { get; set; }
public ActionResult OnGet()
{
return RedirectToPage("/Status/Error");
}
}
}
Reporting\index.cshtml.cs
namespace Reporting.Web.Pages.Reports
{
[TypeFilter(typeof(RouteAccessAttribute), Arguments = new object[] { @"/Report/Index" })]
public class IndexModel : BasePageModel
{
public IndexModel(
ILogger<IndexModel> logger) : base(logger)
{
}
public async Task<IActionResult> OnGet()
{
var clientId = HttpContext.Session.Get<int>("ClientId");
if (clientId > 0)
{
Client = HttpContext.Session.Get<IEnumerable<Client>>("Clients").Where(x => x.Id.Equals(clientId)).FirstOrDefault();
return Page();
}
return RedirectToPage("/Status/Error");
}
}
}
Also, I have the opposite issue with the view, it runs Reporting\index.cshtml but I would instead like to have it run common code under BasePage\index.cshtml.
答案1
得分: 0
Inheritance doesn't really work with ASP.NET Core.
英文:
Inheritance doesn't really work with ASP.NET Core.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论