首页 > 解决方案 > 如何为不同的 SQL Server 文件夹运行相同的 angularjs 应用程序(.NET MVC 5)?

问题描述

我对 AngularJS 和 SQL Server 配置问题非常陌生。

我使用 ASP.NET MVC 5 开发了 AngularJS 应用程序。基本上,前端基于 AngularJS,后端使用 C#。

一旦我们将应用程序放入 SQL Server 中,应用程序就会按预期工作。为了运行该应用程序,我从 Web.Config 文件中获取了 SurveyID = 1(例如)。

现在,我想为多个调查使用不同的 SurveyID 运行相同的应用程序。

a. The server folder is \\Survey with SurveyID = 1
b. The server folder is \\Survey2 with SurveyID = 2
c. The server folder is \\Survey3 with SurveyID = 3
d. The server folder is \\Survey4 with SurveyID = 4

即使应用程序放入不同的文件夹结构,应用程序始终会读取恰好是 \Survey 且 SurveyID = 1 的根文件夹

但是在 MVC Layout_Page 中,我使用的是 HTML 标签,我想知道这是否是根本问题的原因。

现在,我如何根据 Web.Config SurveyID 告诉 angularJS 应用程序哪个是根目录

我该如何解决这个问题?MVC HOME 控制器内部

            DataAccessSurvey data;
            int surveyID;
            public HomeController()
            {
                data = new DataAccessSurvey();
                surveyID =   Convert.ToInt32(ConfigurationManager.AppSettings["SurveyId"]);

            }

    [HttpGet]
        public ActionResult GetSurveyInfoData()
        {
            var surveyData = data.GetSurveyById(surveyID);
            var Survey = new Survey();
            //the rest of the code to populate the Survey related data
        }

在 Angular Controller.jS 中,我调用以下方式

    SurveyServices.getSurveyData()
                    .then(function (result) {
                        //alert("Success getSurveyData GetSurveyInfoData");
                        vm.SurveyInfo = result.data;
                   }

请帮帮我。提前致谢。

标签: c#asp.netangularjsasp.net-web-api

解决方案


看看这个 SO answer about embedding Web.Config values in your JavaScript code。具体来说,您可以使用.constant()此处的文档)将它们注册到您的 AngularJS 应用程序中,并将它们导入到您的组件或控制器中。

angular.module('myApp')
    .constant('SurveyID', <%=ConfigurationManager.AppSettings["SurveyID"] %>)
    .controller('MyCtrl', ['SurveyID', function(SurveyID) { ... }]);

推荐阅读