最佳方式保留由基础控制器提取的信息。

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

Best way to retain information fetch by a Base Controller

问题

我有一个BaseController和BaseViewModel,它们都是其他控制器的基类。它将处理在大多数页面中将要使用的页面信息。

当用户点击侧边菜单时,链接将通过查询字符串处理,其中包含页面ID。页面BaseController将在OnActionExecuted期间使用它来获取相应的数据并将其注入发送到视图的模型中。

我的问题是如何处理对同一控制器的后续请求(该控制器继承自BaseController),而无需担心是否发送了pageId。例如:当进行表单并提交时,即使没有通过表单发送pageId,它仍应知道我仍然在同一个页面上。在索引调用期间使用查询字符串pageId获取的相同页面信息应在回传时发送。

只有在浏览器返回到以前的页面或我明确在查询字符串上指定不同的pageID时,它才应发生更改。

编辑:我还可以通过控制器名称检索页面,而不是pageId,但我不知道如何从BaseController获取原始调用的控制器名称。

英文:

I have a BaseController and BaseViewModel that all others are derived. It will handle page information that will be used in most pages.

When the user clicks on the side menu the link will be handled with a querystring with the page ID. The page BaseController will use that during OnActionExecuted to fetch the corresponding data and inject it into the model sent to the view.

My issue is how to handle subsequent requests to the same controller (that inherits this BaseController) without having to worry if the pageId is sent or not. For example: When doing a Form and submitting it should still know I am on the same page even though pageId is not sent through the form. The postback should send the same page information previously fetched during the index call with queryString pageId.

It should only change when hitting back on the browser to a previous page or when I specifically specify a different pageID on a queryString.

Edit: I am able also to retrieve the page by the controller name instead of pageId, but I don't know how to get the original called controller name from the BaseController.

答案1

得分: 1

抱歉,您无法仅根据HTTP请求来定位当前页面。HTTP是一种无状态协议。尽管可以在同一HTTP连接上发送多个请求,但服务器不会将它们到达相同套接字的情况附加任何特殊含义。

如果您想要跟踪用户从页面到页面的进度,解决这些情况的方法包括:

  • 使用HTTP cookies

  • 服务器端会话

  • 隐藏变量(当当前页面包含表单时)

  • 使用URI编码参数进行URL重写

英文:

Sorry, you can't target the current page just based on the HTTP request. HTTP is a stateless protocol. Even though multiple requests can be sent over the same HTTP connection, the server does not attach any special meaning to their arriving over the same socket.

If you want to track the user's progress from page to page, solutions for these cases include:

  • the use of HTTP cookies

  • server-side sessions

  • hidden variables (when the current page contains a form)

  • URL-rewriting using URI-encoded parameters

答案2

得分: 0

Winson的回答 指导我朝正确的方向前进。

最终,这是我的解决方案。要信任所调用的 Controller,因为我不应该有不同的页面指向同一个 Controller。

public override async void OnActionExecuted(ActionExecutedContext filterContext)
{
    base.OnActionExecuted(filterContext);

    var model = ((Controller)filterContext.Controller)?.ViewData.Model as BaseViewModel;

    if (model is null)
    {
        return;
    }
    var controllerName = filterContext.RouteData.Values["controller"]?.ToString();
    if (string.IsNullOrWhiteSpace(controllerName))
    {
        return;
    }
    controllerName += "Controller";
    await AddPageDataToModel(model, controllerName);

}

话虽如此,如果您对同一个 Controller 有不同的页面,您应该按照 Winson 的回答中描述的会话策略来处理。

英文:

Winson's answer pointed me in the right direction.

In the end, this was my solution. To trust the Controller called as I should not have different pages to the same Controller.

public override async void OnActionExecuted(ActionExecutedContext filterContext)
{
    base.OnActionExecuted(filterContext);

    var model = ((Controller)filterContext.Controller)?.ViewData.Model as BaseViewModel;

    if (model is null)
    {
        return;
    }
    var controllerName = filterContext.RouteData.Values["controller"]?.ToString();
    if (string.IsNullOrWhiteSpace(controllerName))
    {
        return;
    }
    controllerName += "Controller";
    await AddPageDataToModel(model, controllerName);

}

That being said, the way to go if you have different pages for the same Controller, you should follow the session strategy like described in Winson's answer.

huangapple
  • 本文由 发表于 2023年7月17日 22:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705443.html
匿名

发表评论

匿名网友

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

确定