【问题标题】:How do we concat a string in a View file in MVC我们如何在 MVC 的视图文件中连接字符串
【发布时间】:2016-08-22 19:06:00
【问题描述】:

我正在尝试在 MVC 的视图文件中连接字符串。

我们如何做到这一点?

我正在尝试使用应用程序键值作为值:

<%=ConfigurationManager.AppSettings["googleMapKey"].ToString() %>

我希望它位于 Key 的值所在的位置:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6P8xxxxxxxxxxxxxxxxhNMwLG0M&sensor=false"></script>

【问题讨论】:

    标签: asp.net-mvc configuration asp.net-mvc-5 app-config concat


    【解决方案1】:

    假设你使用的是 aspx 视图引擎,你可以使用这个:

    <% string key = ConfigurationManager.AppSettings["googleMapKey"].ToString(); %>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<%= key %>&sensor=false"></script>
    

    如果您使用的是 Razor 视图引擎,那么:

    @{ string key = ConfigurationManager.AppSettings["googleMapKey"].ToString(); }
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=@key&sensor=false"></script>
    

    【讨论】:

      【解决方案2】:

      这样做:

      <script type="text/javascript" src="<%= string.Format("https://maps.googleapis.com/maps/api/js?key={0}&sensor=false",ConfigurationManager.AppSettings["googleMapKey"].ToString()) %>">
      

      您实际上正在使用“string.Format”命令,顺便说一下,我相信这是 .Net 中最有益的命令之一。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多