Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Working with Settings #806


User avatar
0
alukaszewski created

I have added some additional settings to general and can successfully set and store these in the database. I just duplicated how the WebSiteRootAddress setting worked.

If I have a controller, how do I retrieve a setting? I have tried having "using abp.configuration;" at the top of the controller, and trying something like

var value2 = SettingManager.GetSettingValue("somestoredsetting");

results in VS showing "'SettingManager' does not contain a definition for 'GetSettingValue'".

What am I missing? Do I need to use the @Model method of code that the Settings.cshtml page uses to retrieve settings?


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    In an MVC Controller, you can directly use SettingManager. Add "using Abp.Configuration;" to your file, then use

    var value = SettingManager.GetSettingValue("YOUR_SETTING_NAME");
    

    GetSettingValue is an extension method declared in Abp.Configuration namecpase. Can you try it again?

  • User Avatar
    0
    alukaszewski created

    Yes that is exactly what I am trying, it should be simple.

    using Abp.Configuration;
    
    var value = SettingManager.GetSettingValue("YOUR_SETTING_NAME");
    

    results in VS build failure due to:

    'SettingManager' does not contain a definition for 'GetSettingValue'.

    The GetSettingValue is highlighted in the VS editor as that cannot be found as a method/whatever of SettingManager.

    I am using Mpa.

    If I look at the inbuilt Mpa/Controllers/SettingsController.cs, I get more extension methods when I try var value = SettingManager.

    I get .GetSettingValueAsync, .GetSettingValueForApplicationAsync etc.

    Are you sure I only need using Abp.Configuration? I can see that the SettingsController.cs has the following;

    using System.Threading.Tasks;
    using System.Web.Mvc;
    using Abp.Configuration.Startup;
    using Abp.Web.Mvc.Authorization;
    using MyProject.Authorization;
    using MyProject.Configuration.Tenants;
    using MyProject.Web.Controllers;
    

    Do I need to any of those? I did try some but still I do not have the .GetSettingValue extension method when I try SettingManager.

    Thanks,

    Andy

  • User Avatar
    0
    alukaszewski created

    Further, if I try;

    var value1 = Abp.Configuration.SettingManager.

    Then I only get '.ApplicationSettingsCacheKey', 'Equals' and 'ReferenceEquals' methods. Weird.

  • User Avatar
    0
    alukaszewski created

    ... and if I try

    var value1 = SettingManagerExtensions.GetSettingValue("MyValue")

    Then although I get to see/choose the .GetSettingValue method, I get error "No overload for method 'GetSettingValue' takes 1 arguments"

    So I'm baffled.

  • User Avatar
    0
    alukaszewski created

    Hikal,

    Could you please provide a basic code example for an Mpa page controller, that is able to use the SettingManager.GetSettingValue method and return a value to the client.

    For example, here is a very basic controller that returns a string;

    using Abp.Configuration;
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MyProject.Web.Controllers
    {
        public class HelloWorldController : Controller
        {
            public string Index()
            {
                return "Hello World!";
            }
        }
    
    }
    

    How do I make that return a stored setting string, such as the SMTP Host? I thought I would be able to simply use the SettingManager.GetSettingValue before returning the value, but I cannot understand how.

    Thanks.

  • User Avatar
    0
    hikalkan created
    Support Team

    You should inherit your controllers from AbpController class (which defined the SettingManager), not from Controller class. As better, you should inherit from your application specific controller base. Check other pre-build MVC Controller (for example, RoleController: <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/master/src/MyCompanyName.AbpZeroTemplate.Web/Areas/Mpa/Controllers/RolesController.cs">https://github.com/aspnetzero/aspnet-ze ... troller.cs</a>) to see the base controller class.