在 ASP.Net MVC 2 中的整个站点中保留查询字符串参数

发布于 2024-09-01 17:38:27 字数 572 浏览 4 评论 0原文

http:www.site1.com/?sid=555

我希望无论发布表单还是单击链接,都能够保留 sid 参数和值。

如果用户导航到实现分页的视图,则应在 sid 之后添加查询字符串中的其他参数。

http:www.site1.com/?sid=555&page=3

如何在 Asp.Net Mvc 2 中完成此操作?

[编辑]

我在顶部提到的 url 将是应用程序的入口点,因此 sid 将包含在链接中。

在应用程序链接中,例如:

<%= Html.ActionLink("Detail", "Detail", new { controller = "User", 
                                              id = item.UserId })%>

应该转到:
http:www.site1.com/user/detail/3?sid=555

这个问题与戴夫提到的不同,因为查询字符串参数在整个站点中持续存在。

http:www.site1.com/?sid=555

I want to be able to have the sid parameter and value persist whether a form is posted or a link is clicked.

If the user navigates to a view that implements paging, then the other parameters in the querystring should be added after the sid.

http:www.site1.com/?sid=555&page=3

How can I accomplish this in Asp.Net Mvc 2?

[Edit]

The url I mentioned on top would be the entry point of the application, so the sid will be included in the link.

Within the application links like:

<%= Html.ActionLink("Detail", "Detail", new { controller = "User", 
                                              id = item.UserId })%>

should go to:
http:www.site1.com/user/detail/3?sid=555

This question is different than what Dave mentions, as the querystring parameter is persisting throughout the site.

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

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

发布评论

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

评论(2

空城仅有旧梦在 2024-09-08 17:38:27

首先,我想说,如果该值需要在整个会话中保留,那么您应该将其存储在会话中,并检查它在每个操作调用中是否仍然有效。这可以通过添加到所需的控制器/操作的自定义操作属性来完成。如果该值是必需的,那么当检查该值时,如果不存在或已过期,您可以重定向到登录页面或类似页面。

不管怎样,这就是说我想我可以让它发挥作用。我的第一个想法是创建一个自定义操作过滤器属性,该属性获取查询字符串的值并将其存储在 OnActionExecuting 的会话中,然后 OnResultExecuted 将密钥添加回查询字符串。但由于 Request 中的 QueryString 是一个只读集合,因此您无法直接执行此操作。

那么,现在您可以使用什么?

选项 #1 - 将其手动添加到对 Html.ActionLink() 的所有调用中,

或者...

选项 #2 - 覆盖 ActionLink 的版本,该版本会自动为您添加值。这可以像这样实现。但我不建议这样做。

从自定义属性开始。

public class PersistQueryStringAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var sid = filterContext.RequestContext.HttpContext.Request.QueryString["sid"];

        if (!string.IsNullOrEmpty(sid))
        {
            filterContext.RequestContext.HttpContext.Session["sid"] = sid;
        }

        base.OnActionExecuting(filterContext);
    }
}

这一切所做的就是检查请求查询字符串中是否有所需的密钥,如果可用,则将其添加到会话中。

然后,您将 ActionLink 扩展方法重写为您自己的扩展方法之一,该方法会添加值。

public static class HtmlHelperExtensions
{
    public static MvcHtmlString ActionLink<TModel>(this HtmlHelper<TModel> helper, string text, string action, string controller, object routeValues)
    {
        var routeValueDictionary = new RouteValueDictionary(routeValues);

        if (helper.ViewContext.RequestContext.HttpContext.Session["sid"] != null)
        {
            routeValueDictionary.Add("sid", helper.ViewContext.RequestContext.HttpContext.Session["sid"]);    
        }

        return helper.ActionLink(text, action, controller, routeValueDictionary, null);
    }
}

在将要调用的每个操作上应用该属性(或将其应用于控制器),例如:

[PersistQueryString]
public ActionResult Index()
{
    ViewData["Message"] = "Welcome to ASP.NET MVC!";

    return View();
}

注意

当查询值被放入会话中时,它将在会话的生命周期内应用。如果您想检查该值是否存在并且每个请求都相同,您将需要在属性覆盖方法中进行一些检查。

最后

我这样做纯粹是为了“能做到吗”练习。我强烈建议不要这样做。

Firstly, I'd say if the value needs to be persisted throughout the session then you should store it in Session and check that its still valid on each action call. This can be done through a custom action attribute you add to the controller / actions required. If the value is required then when the value is checked you can re-drect to a login page or similar if not present or its expired.

Anyway, that said I thought I would have a crack at getting it working. My first thought would be to create a custom action filter attribute which took the value of the querstring and stored it in session in OnActionExecuting and then OnResultExecuted would add the key back to the querystring. But as QueryString in Request is a read-only collection you can't do it directly.

So, whats now available to you?

Option #1 - Add it to all calls to Html.ActionLink() manually

or ...

Option #2 - Override a version of ActionLink which automatically adds the value for you. This can be achived like so. I wouldn't recommend doing this though.

Start off with the custom attribute.

public class PersistQueryStringAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var sid = filterContext.RequestContext.HttpContext.Request.QueryString["sid"];

        if (!string.IsNullOrEmpty(sid))
        {
            filterContext.RequestContext.HttpContext.Session["sid"] = sid;
        }

        base.OnActionExecuting(filterContext);
    }
}

All this does is check the request querystring for the required key and if its available add it into the session.

Then you override ActionLink extention method to one of your own which adds the value in.

public static class HtmlHelperExtensions
{
    public static MvcHtmlString ActionLink<TModel>(this HtmlHelper<TModel> helper, string text, string action, string controller, object routeValues)
    {
        var routeValueDictionary = new RouteValueDictionary(routeValues);

        if (helper.ViewContext.RequestContext.HttpContext.Session["sid"] != null)
        {
            routeValueDictionary.Add("sid", helper.ViewContext.RequestContext.HttpContext.Session["sid"]);    
        }

        return helper.ActionLink(text, action, controller, routeValueDictionary, null);
    }
}

On each of the action which is going to be called apply the attribute (or apply it to the controller), eg:

[PersistQueryString]
public ActionResult Index()
{
    ViewData["Message"] = "Welcome to ASP.NET MVC!";

    return View();
}

Note

As the query value gets put into session it will be applied for the life of the session. If you want to check that the value is there and the same each request you will need to do some checking in the attribute overridden method.

Finally

I've purely done this as a "can it be done" exercise. I would highly recommend against it.

感情旳空白 2024-09-08 17:38:27

可能重复:

如何在 asp. net mvc?

我同意上面链接的问题的已接受答案。查询字符串参数不是为数据持久性而设计的。如果某个设置(即sid=555)旨在通过会话持续存在,请使用会话状态或模型来保存该数据以供跨请求使用。

Possible Duplicate:

How do you persist querystring values in asp.net mvc?

I agree with the accepted answer to the question linked above. Querystring parameters are not designed for data persistence. If a setting (i.e. sid=555) is intended to persist through a session, use Session state or your Model to save that data for use across requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文