.net core Web Page不路由到具有OnGet()方法的继承类

huangapple go评论60阅读模式
英文:

.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.

huangapple
  • 本文由 发表于 2023年3月1日 13:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75599899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定