How do you access application variables in asp.net mvc 3 razor views?

前端 未结 7 1518
不思量自难忘°
不思量自难忘° 2020-12-14 16:00

I set a Application variable in my global.asa.cs with:

    protected void Application_Start()
    {
        ...

        // load all application settings
            


        
相关标签:
7条回答
  • 2020-12-14 16:06

    You should be able to access this via HttpContext.Current.Application[], however MVC best practices would state that you should probably consider passing this through your View Model.

    0 讨论(0)
  • 2020-12-14 16:06
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
    
            var e = "Hello";
            Application["value"] = e;
        }
    

    @HttpContext.Current.Application["value"]

    0 讨论(0)
  • 2020-12-14 16:07

    You can get the current Application using the automatically generated ApplicationInstance property:

    @ApplicationInstance.Application["LICENSE_NAME"]
    

    However, this logic does not belong in the view.

    0 讨论(0)
  • 2020-12-14 16:20
    @HttpContext.Current.Application["someindex"]
    
    0 讨论(0)
  • 2020-12-14 16:21

    I had this issue in an MVC controller and had to make fully qualified HttpContext for it to work ..

    System.Web.HttpContext.Current.Application["VarName"]
    
    0 讨论(0)
  • 2020-12-14 16:26

    Building on @Darin-Dimitrov pattern answered above, I passed a model into a partial view, which I loaded into a _Layout page.

    I needed to load a web page from an external resource on Application Load, which will be used as the header navigation across multiple sites. This is in my 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;
    }
    

    Here is my controller Action for the partial view.

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

    I loaded the partial view in the _Layout page like this.

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

    The partial view _Header.cshtml is very simple and just loads the html from the application variable.

    @model Models.HeaderModel
    @MvcHtmlString.Create(Model.NavigationHtml)
    
    0 讨论(0)
提交回复
热议问题