开发者

Read value from Web config,pass it to controller, put it in a viewbag and then read the values in View?

开发者 https://www.devze.com 2023-04-13 02:43 出处:网络
I have a simple requirement where I have 2 date values which I will be using in my view(hardcoded right now) to be read from Web config file.

I have a simple requirement where I have 2 date values which I will be using in my view(hardcoded right now) to be read from Web config file. I got some inputs saying these values have to be passed to controller, put them in a view bag, and then read in View. Since this is the 1st time I am working on MVC, please help me with how to proceed with this. Detailed code in each of the places( Web config, controller, and View) would be helpful.

Thanks in advance, Ad开发者_开发技巧arsh


Configure Unity as IOC by creating your own ControllerFactory http://weblogs.asp.net/shijuvarghese/archive/2008/10/24/asp-net-mvc-tip-dependency-injection-with-unity-application-block.aspx

Then at start-up register your web.config settings with the unity container in an object of your setting class and your controllers can then take it in constructor and it will automatically be injected.

public class HomeController : Controller
{

    WebConfigSettings settings;
    public HomeController(WebConfigSettings settings) // <-- coming via IOC
    {
            this.settings = settings;
    }

    public ActionResult Index()
    {

        ViewBag.SomeSetting = settings.SomeSetting;

        return View();
    }
}

You can then set the values from that in your view bag easily.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号