2011-03-12 60 views
27

我设置一个应用程序变量在我global.asa.cs有:如何访问asp.net mvc 3 razor视图中的应用程序变量?

protected void Application_Start() 
    { 
     ... 

     // load all application settings 
     Application["LICENSE_NAME"] = "asdf"; 

    } 

,然后尝试这样我的Razor视图访问:

@Application["LICENSE_NAME"] 

,并得到这个错误:

Compiler Error Message: CS0103: The name 'Application' does not exist in the current context 

什么是正确的语法?

+0

Application变量?如.config文件中的appSettings一样? (请更具体的说明) – 2011-03-12 23:28:27

+3

@Brad,他意思是应用程序状态变量,如果你看看它的代码是非常特定的。 – 2011-03-12 23:32:10

+0

@Brad:我认为他意味着应用程序状态对象中的值。 – Misko 2011-03-12 23:32:48

回答

65

视图不应该从某处拉取数据。他们应该使用从控制器操作中以视图模型的形式传递给它们的数据。所以,如果你需要在一个视图中使用这些数据正确的方式做它是定义视图模型:

public class MyViewModel 
{ 
    public string LicenseName { get; set; } 
} 

有你的控制器动作不管它需要来填充它填充它(为关注更好的分离您可以使用存储库):

public ActionResult Index() 
{ 
    var model = new MyViewModel 
    { 
     LicenseName = HttpContext.Application["LICENSE_NAME"] as string 
    }; 
    return View(model); 
} 

终于有强类型视图显示这个信息给用户:

<div>@Model.LicenseName</div> 

这是正确的MVC模式和塔t是如何完成的。

避免将数据视为有害物的视图,因为今天它的应用程序状态,明天它是一个foreach循环,下周它是一个LINQ查询,并且很快你就会在你的视图中编写SQL查询。

+3

+1不仅指向问题的解决方案,还更好地遵循MVC模式。 – 2011-03-12 23:31:44

+0

如果您需要重新使用某些代码(如在页眉菜单中),您还可以创建单独的ActionResult来呈现部分视图。我需要从我的_Layout页面将模型发送到PartialView,并且需要可重用的代码。我遵循这种模式,并使用**'@ {Html.RenderAction(“ActionName”,“ControllerName”);}'**。该行动返回了部分视图。 – 2011-10-07 13:41:14

+0

嗨,我使用相同的模式,但一些代码使用静态变量,而不是应用程序状态。我真的想知道什么是正确的方式? – 2012-01-16 07:27:23

22
@HttpContext.Current.Application["someindex"] 
5

你应该能够通过HttpContext.Current.Application[]访问此,但是MVC的最佳实践将陈述你也许应该考虑通过这种通过您的视图模型。

6

您可以使用自动生成的ApplicationInstance property获取当前的应用程序:

@ApplicationInstance.Application["LICENSE_NAME"] 

然而,这种逻辑并不在视图中的归属。

+0

这可行,但Visual Studio和or Resharper不喜欢它。 IDE更喜欢@ HttpContext.Current.Application [“someindex”],即使它们不是最佳实践。 – 2011-10-07 13:22:35

4

建立在@ Darin-Dimitrov模式上面回答,我将一个模型传递到一个局部视图,我加载到一个_Layout页面。

我需要从应用程序加载的外部资源加载网页,该网页将用作跨多个网站的页眉导航。这是在我的Global.asax.cs

protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 

    RegisterGlobalFilters(GlobalFilters.Filters); 
    RegisterRoutes(RouteTable.Routes); 

    Application["HeaderNav"] = GetHtmlPage("https://site.com/HeaderNav.html"); 
} 

static string GetHtmlPage(string strURL) 
{ 
    string strResult; 
    var objRequest = HttpWebRequest.Create(strURL); 
    var objResponse = objRequest.GetResponse(); 
    using (var sr = new StreamReader(objResponse.GetResponseStream())) 
    { 
     strResult = sr.ReadToEnd(); 
     sr.Close(); 
    } 
    return strResult; 
} 

这是我的控制器部分视图的操作。

public class ProfileController : BaseController 
{ 
    public ActionResult HeaderNav() 
    { 
     var model = new Models.HeaderModel 
     { 
      NavigationHtml = HttpContext.Application["HeaderNav"] as string 
     }; 
     return PartialView("_Header", model); 
    } 
} 

我像这样在_Layout页面中加载了局部视图。

<div id="header"> 
    @{Html.RenderAction("HeaderNav", "Profile");} 
</div> 

局部视图_Header.cshtml非常简单,只是装载来自应用程序变量的HTML。

@model Models.HeaderModel 
@MvcHtmlString.Create(Model.NavigationHtml) 
1
protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 

     var e = "Hello"; 
     Application["value"] = e; 
    } 

@ HttpContext.Current.Application [ “值”]

相关问题