查询字符串和

问题描述:

的RenderAction

我设置在我的HTML标签的类时发送特定的查询字符串参数,现在我做这样的(Razor视图母版页):查询字符串和

@if (HttpContext.Current.Request.QueryString.AllKeys.Contains("Foo") && HttpContext.Current.Request.QueryString["Foo"] == "Bar") { 
    //Do something when Foo=Bar (like http://server/route?Foo==Bar) 
    <html class="bar-class"> 
} 
else { 
    //Normal html tag 
    <html> 
} 

工作正常正常的请求,但不是当我打电话使用的的RenderAction页面,就像

//Other view, the one requested by the user 
@Html.RenderAction("Index", "Route", new {Foo="Bar"}) 

一些四处寻找我已经认识到只有一个实际的HttpContext,这意味着HttpContext.Current指向第一个请求。那么 - 如何获取子请求的查询字符串数据?

谢谢! /Victor

+0

您在'new {Foo =“Bar”}''中缺少一个报价。 – 2012-01-05 12:42:16

+0

谢谢Pankaj,固定。 – Victor 2012-01-09 10:01:07

至少现在我通过使用TempData字典和使用后删除值解决了问题,但我仍然对更好的解决方案感兴趣。似乎应该有一种方式来获得routedata通过...

/Victor

您可以使用string作为您的Model,而不使用查询字符串。

@model string 
@if (!string.IsNullOrWhiteSpace(Model) && Model == "Bar") { 
    //Do something when Foo=Bar (like http://server/route?Foo==Bar) 
    <html class="bar-class"> 
} 
else { 
    //Normal html tag 
    <html> 
} 

public ActionResult Route(string foo){ 
    return View(foo); 
} 
+0

问题是我需要这个通用 - 我将在_every_请求的布局中使用,并且模型需要由实际视图指定。所以我真的需要访问原始查询字符串(或similair)...也许过滤器可能是一个想法,但我不知道如何... – Victor 2012-01-09 10:00:22