ASP.NET:客户端的根运算符的相对路径('〜')

时间:2015-08-10 03:01:20

标签: asp.net ajax path

我已经用asp.net实现了一个网页。

它有一些ajax功能。

在ajax函数中,从服务器端webMethod获取图像路径。

图像路径由根运算符组成,例如"〜/ Images / Icons / SendEmail.png"。

在客户端,我想将图像路径设置为img元素。

如何从此相对路径设置图像?

这是我的代码段。

请参考这个并给我一些建议。提前谢谢。

Clien方

function DrawImage() {        
    $.ajax({
        type: 'POST',
        url: '../Management/GetImage',
        data: '{Index: "' + Index + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function (resp) {
            if (resp.d == null) {
                return;
            }
            var ImagePath = resp.d;

            var image = document.createElement('img');
            image.src = ImagePath; // e.g. "~/Images/Image.png"
            $(imageDiv).append(image);
        },

        error: function (msg) {
            alert("Failed to Image: " + msg.statustext);
        }
    });
}

服务器端WebMethod

    [WebMethod]
    public static string GetImage(string Index)
    {
        string conStr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(conStr);
        SqlCommand command = new SqlCommand();
        command.Connection = conn;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "Select_ImagePath";
        command.Parameters.AddWithValue("@Index", Index);

        string imgPath = "";

        try
        {
            conn.Open();
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.Read())
            {
                imgPath = (string)dataReader["Image_Path"]; // e.g. "~/Images/Image.png"
            }
        }
        catch (Exception err)
        {

        }
        finally
        {
            conn.Close();
        }

        return imgPath;
    }

1 个答案:

答案 0 :(得分:0)

我通过在javascript中实现一些函数来解决这个问题,如下所示。

  .state('8week.visitorId', {
    url:'/8week/:visitorId',
    templateUrl: 'app/8week/8week-page.tmpl.html',
    controller: 'VideoCtrl',
    controllerAs: 'controller'
  })