Ep

Ep

How to Get Key Value From Web Config in ASP.Net MVC

Read value from web.config C#

Web config appsettings section is used to store user defined values such as connection strings which is used through out the project for database connection. This tip shows that how to get key value from web config in asp net in two different ways at client side using javascript and at server side in code behind. Find the code snippet below :-
 <appSettings>
    <add key="vDirectory" value="demo" />
  </appSettings>

MainController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC4_ReadAppSettingValue.Controllers
{
    public class MainController : Controller
    {
        // GET: /Main/      
        public ActionResult GetAppSettingsValueClientSide()
        {
            return View();
        }
        public ViewResult GetAppSettingsValueServerSide()
        {
            ViewData["vDir"] = System.Configuration.ConfigurationManager.AppSettings["vDirectory"].ToString();
            return View();
        }
    }
}
c# .net mvc, mvc web,dot net mvc, mvc dot net,asp mvc, mvc website,
read value from web.config c#
GetAppSettingsValueServerSide.cshtml
@{
       ViewBag.Title = "Get value from web.config AppSettings Server Side";
   }
<h2>
    Get value from web.config AppSettings Server Side
</h2>
<div>
    @ViewData["vDir"]
</div>

If you want to read web.config APP key settings in HTML at Client side, use the below code in your asp.net mvc web application

GetAppSettingsValueClientSide.cshtml
<!DOCTYPE html>
<html>
<head>
    <title>MVC 4 - Get value from web.config AppSettings using jQuery</title>
    <script src="~/Scripts/utility.js" type="text/javascript"></script>
    <script type="text/javascript">
        var Vdir = '@System.Web.Configuration.WebConfigurationManager.AppSettings["vDirectory"]';     
    </script>
</head>
<body>
    <h3>
        Click below to get value from web.config AppSettings
    </h3>
    <a href="#" onclick="GetAppSettingsValue();">Get Appsettings Value</a>
</body>
</html>


utility.js
function GetAppSettingsValue() {
    alert(Vdir);
}
get value from web.config appsettings c#,access web.config appsettings, web.config app settings, asp.net web config appsettings, asp.net appsettings read
mvc view get value from web.config using at client side
How to Get Key Value From Web Config in ASP.Net MVC Reviewed by Ravi Kumar on 6:19:00 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.