asp.net-mvc - 在mvc中维护所有页面中的查询字符串

标签 asp.net-mvc query-string

我需要在我的 asp.net mvc(C#) 应用程序的所有页面中维护查询字符串。

例如:
我会叫一个页面www.example.com?Preview=True .无论我在 www.example.com 中单击哪个页面,都应该维护查询字符串.即当我点击 About us页面在www.example.com , url 应该是 www.example.com/AboutUs?Preview=True
我怎样才能做到这一点?什么是进行这种常见操作的最佳位置。?

最佳答案

也许您需要自定义路线?:

public class PreviewRoute : System.Web.Routing.Route
{
    ...

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        var preview = System.Web.HttpContext.Current.Session["Preview"];

        if (!values.ContainsKey("Preview"))
            values.Add("Preview", preview);

        var path = base.GetVirtualPath(requestContext, values);

        return path;
    }
}

}

套装Session["Preview"]任何时候,您都可以通过 ?Preview=True 获得所有网址。 :
System.Web.HttpContext.Current.Session.Add("Preview", true);

更新:

在 Global.asax.cs 中使用此路由:
routes.Add("Default",
    new PreviewRoute("{controller}/{action}/{id}", new MvcRouteHandler()) {
        Defaults = new RouteValueDictionary(
            new { controller = "Home", action = "Index", id = "" }
        )
    }
);

代替:
routes.MapRouteLowercase(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

你也可以试试这个扩展:
public static class CustomRouteExtensions
{
    public static void MapPreviewRoute(this RouteCollection routes, string name, string url, object defaults) {
        routes.MapPreviewRoute(name, url, defaults, null);
    }

    public static void MapPreviewRoute(this RouteCollection routes, string name, string url, object defaults, object constraints) {
        if (routes == null) {
            throw new ArgumentNullException("routes");
        }

        if (url == null) {
            throw new ArgumentNullException("url");
        }

        var route = new PreviewRoute(url, new MvcRouteHandler()) {
            Defaults = new RouteValueDictionary(defaults),
            Constraints = new RouteValueDictionary(constraints)
        };

        if (String.IsNullOrEmpty(name)) {
            routes.Add(route);
        }
        else {
            routes.Add(name, route);
        }
    }
}

在 Global.asax.cs 中:
routes.MapPreviewRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

关于asp.net-mvc - 在mvc中维护所有页面中的查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1789453/

相关文章:

c# - .NET 中的查询字符串是好的做法吗?

php - 是否可以检测到空请求?

c# - 此特定场景中的最佳缓存策略

asp.net-mvc - IIS 8.5 - 应用程序初始化不起作用

c# - 解决 ASP.NET MVC View 中的 "DataContext accessed after Dispose"错误

jquery - 在MVC中添加动态表单内容

asp.net-mvc - 如何使用 MVC(4)创建(图像)标题 Logo

.net - 在 OpenRasta 中是否可以模式匹配多个键/值对?

c# - 查看 ASP.NET MVC3 中的模型模式和用法(同时使用 EF 4.1)

php - 如何阻止处理用户生成的查询字符串