控制器动作历史记录

发布于 2024-12-19 16:01:36 字数 494 浏览 0 评论 0原文

我的 ASP.NET MVC Intranet 应用程序有一个数据存储库,它使用当前用户的 Windows 登录名来检索属于用户默认主站点的数据。登录信息包含站点标识符。存储库类是访问 HttpContext.Current.User.Identity 并从登录中提取站点 id 的唯一位置。因此,该应用程序仅提供一个用户站点的数据。现在,我们的要求已更改,用户可以通过单击“站点”菜单中的不同站点名称链接来查看其他站点的数据。站点菜单位于母版页中。当用户单击任何站点链接时,需要更新站点范围,然后使用新的站点范围重新执行单击站点链接之前执行的操作。例如,如果用户执行请求列表,默认情况下它将仅显示来自用户默认站点的请求。当用户点击另一个站点时,它将显示该站点的请求。应用程序中有很多控制器操作,我不喜欢修改每个控制器的每个操作和现有路由。我正在考虑创建一个带有设置站点范围的操作的新控制器,但我不知道如何使其知道在执行新控制器的操作之前执行了哪些操作。我应该使用会话变量来保持范围变化吗?操作过滤器是重构我的应用程序的更好方法吗?任何有关代码示例的建议都将受到赞赏。谢谢。

My ASP.NET MVC intranet app has a data repository that uses current user's Windows login to retrieve data that belongs to the user's default home site. The login contains a site identifier. The repository class is the only place that accesses the HttpContext.Current.User.Identity and extracts the site id from the login. Therefore, the application only serves one user site's data. Now our requirements have changed that users can view other sites' data by clicking a different site name link from a Site menu. The Site menu is in the Master page. When the user clicks on any site link, the site scope needs to be updated then it re-executes the action that was executed before the site link is clicked with the new site scope. For example, if a user executes Requests List, by default it will show only the requests from the user's default site. When the user clicks another site, it will show t hat site's requests. There are many controller actions in the app, I prefer not modifying each action of each controller and existing routes. I am thinking about creating a new controller with an action to set the Site scope, but I don't know how to make it aware what action was executed before the new controller's action is executed. Should I use Session variable to keep the scope change? Is an Action Filter a better way to re-factor my app? Any suggestion with code sample is appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○闲身 2024-12-26 16:01:36

我最终创建了一个新的控制器,其中包含一个创建会话变量的操作,然后执行重定向。在存储库中,检查会话变量以查看它是否与用户的主站点相同。如果会话变量的作用域不同,则返回会话变量的值。 SiteController.Index() 的 Index.aspx 返回一个 UserControl。

<%@ Control Language="C#"    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<VETS.Models.Site>>" %>
<ul id="menu">
    <% foreach (var item in Model)
       { %>

    <li><%: Html.ActionLink(item.SiteName, "SetSiteScope", new { siteID = item.SiteID })%></li>
    <%
       } %>
</ul>

public class SiteController : Controller 
{
    public ActionResult SetSiteScope(short siteID)
    {
        HttpContext.Session.Add("CurrentSiteID",siteID);
        //var routeData = ControllerContext.RouteData;
        //routeData.Route.ToString();
        //var actionName = ControllerContext.RouteData.GetRequiredString("action");
        //return RedirectToAction(actionName, "Transfers");
        Uri prevURL = HttpContext.Request.UrlReferrer;
        return Redirect(prevURL.ToString());
    }
}

public class vRepository : IvRepository
{
    public Site CurrentSite()
    {

      if (HttpContext.Current.Session != null && HttpContext.Current.Session["CurrentSiteID"] != null)
      {
        targetSiteID = short.Parse(HttpContext.Current.Session["CurrentSiteID"].ToString());
        targetSite = SiteList().Single(s => s.SiteID == targetSiteID);
      }
      return targetSite;
    }
}

I ended up creating a new controller with an action to create a session variable after which a redirect is executed. In the Repository, the session variable is checked to see if it is the same as the user's home site. The session variable's value is return if they are different scope. The Index.aspx of the SiteController.Index() return a UserControl.

<%@ Control Language="C#"    Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<VETS.Models.Site>>" %>
<ul id="menu">
    <% foreach (var item in Model)
       { %>

    <li><%: Html.ActionLink(item.SiteName, "SetSiteScope", new { siteID = item.SiteID })%></li>
    <%
       } %>
</ul>

public class SiteController : Controller 
{
    public ActionResult SetSiteScope(short siteID)
    {
        HttpContext.Session.Add("CurrentSiteID",siteID);
        //var routeData = ControllerContext.RouteData;
        //routeData.Route.ToString();
        //var actionName = ControllerContext.RouteData.GetRequiredString("action");
        //return RedirectToAction(actionName, "Transfers");
        Uri prevURL = HttpContext.Request.UrlReferrer;
        return Redirect(prevURL.ToString());
    }
}

public class vRepository : IvRepository
{
    public Site CurrentSite()
    {

      if (HttpContext.Current.Session != null && HttpContext.Current.Session["CurrentSiteID"] != null)
      {
        targetSiteID = short.Parse(HttpContext.Current.Session["CurrentSiteID"].ToString());
        targetSite = SiteList().Single(s => s.SiteID == targetSiteID);
      }
      return targetSite;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文