【问题标题】:ASP.NET MVC - Can't locate image file when run with IISASP.NET MVC - 使用 IIS 运行时找不到图像文件
【发布时间】:2010-12-30 20:27:22
【问题描述】:

我为 DatePicker 创建了一个 HTMLExtension 方法。我从视图中引用它如下(图像位置见粗体文本):

        <label for="Effective Start Date", class="codeValuesEditLabel"> Effective Start Date (YYYY/MM/DD): </label>
    <%= Html.DatePicker("CodeValueNewEditModel_EffectStartDate", "CodeValueNewEditModel.EffectStartDate", "../../Content/images/calendar.gif",
            Model.codeValueNewEditModel.EffectStartDate) %>

HTMLExtension 方法:

public static string DatePicker(this HtmlHelper helper, string id, string name, **string imageUrl**, object date)
    {
        StringBuilder html = new StringBuilder();

        // Build our base input element    
        html.Append("<input type=\"text\" id=\"" + id + "\" name=\"" + name + "\"");

        // Model Binding Support    
        if (date != null)
        {
            string dateValue = String.Empty;
            if (date is DateTime? && ((DateTime)date) != DateTime.MinValue)
                dateValue = ((DateTime)date).ToString("yyyy/MM/dd"); 
            else if (date is DateTime && (DateTime)date != DateTime.MinValue)
                dateValue = ((DateTime)date).ToString("yyyy/MM/dd"); 
            else if (date is string)
                dateValue = (string)date;

            html.Append(" value=\"" + dateValue + "\"");
        }

        // We're hard-coding the width here, a better option would be to pass in html attributes and reflect through them    
        // here ( default to 75px width if no style attributes )    
        html.Append(" style=\"width: 75px;\" />");

        // Now we call the datepicker function, passing in our options.  Again, a future enhancement would be to    
        // pass in date options as a list of attributes ( min dates, day/month/year formats, etc. )    

        // If there is no image given, open calendar by clicking on text box
        if (imageUrl == null)
        {
            html.Append("<script type=\"text/javascript\">$(document).ready(function() { $('#" + id + "').datepicker( { dateFormat: 'yy/mm/dd', changeMonth: 'true', changeYear: 'true', duration: 0 }); });");      
        }
        else
        {
            html.Append("<script type=\"text/javascript\">$(document).ready(function() { $('#" + id + "').datepicker( { dateFormat: 'yy/mm/dd', showOn: 'button', changeMonth: 'true', changeYear: 'true', buttonImage: '" + **imageUrl** + "', duration: 0 }); });");
        }
        html.Append("</script>");

        return html.ToString();
    }

在开发服务器上运行时一切正常,但使用 IIS 运行时,找不到图像。通常我会使用“Url.Content”来解决这些问题,但我不能将它放在 Javascript 中。有人有什么建议吗?

【问题讨论】:

  • 查看两个不同主机位置(iis 和开发)的输出(查看源代码)。如果这不能帮助您解决问题,请在此处发布。
  • 在帮助器中构造javascript时,为什么不能在imageUrl参数周围使用Url.Content
  • 我把脚本标签修改为: buttonImage: '' 并修改视图传入如下路径( imageURL): "~/Content/images/calendar.gif" 但是在开发服务器上找不到该图像。你知道我做错了什么吗?

标签: model-view-controller asp.net-mvc-2


【解决方案1】:

你可能想要改变:

<%= Html.DatePicker("CodeValueNewEditModel_EffectStartDate", "CodeValueNewEditModel.EffectStartDate", "../../Content/images/calendar.gif",             Model.codeValueNewEditModel.EffectStartDate) %> 

到:

<%= Html.DatePicker("CodeValueNewEditModel_EffectStartDate", "CodeValueNewEditModel.EffectStartDate", Url.Content("~/Content/images/calendar.gif"),             Model.codeValueNewEditModel.EffectStartDate) %> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    相关资源
    最近更新 更多