【问题标题】:How to run the same angularjs apps (.NET MVC 5) for a different SQL Server folder?如何为不同的 SQL Server 文件夹运行相同的 angularjs 应用程序(.NET MVC 5)?
【发布时间】:2019-11-13 01:19:21
【问题描述】:

我对 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.net angularjs asp.net-web-api


    【解决方案1】:

    查看this SO answer,了解在您的 JavaScript 代码中嵌入 Web.Config 值。具体来说,您可以使用 .constant() (docs here) 将它们注册到您的 AngularJS 应用程序中,然后将它们导入到您的组件或控制器中。

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

    【讨论】:

    • 我确实从 Web.Config 获取了 SurveyID 值以填充调查问题。但这并不能解决我的问题。我想我怎样才能告诉应用程序从不同的服务器文件夹中读取调查 ID?谢谢
    • 除非我知道文件夹结构以及您与 API 通信的位置,否则恐怕无法帮助您。可能您使用的是$http$resource,对吧?
    • 你是对的,我使用 $http 来获取 SurveyID。
    • 你的意思是“不同的文件夹”,surveyID 是 URL 的一部分?因为在这种情况下,你可以像 path/to/Survey${surveyID} 这样插入变量
    • 它是一个调查应用程序。我想同时为不同的调查运行此应用程序。因此,IIS 服务器为每个调查都有一个单独的文件夹。对于每个调查,id 存储在 Web.Config 中。 ID 不是 URL 的一部分。根是Survey,结构类似于Survey/Survey1、Survey/Survey2、Survey/Survey3。
    猜你喜欢
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多