ASP.NET MVC - 使用 IIS 运行时无法找到图像文件

发布于 2024-10-09 18:30:46 字数 2673 浏览 0 评论 0原文

我为 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 中。有人有什么建议吗?

I created a HTMLExtension method for a DatePicker. I reference it from the view as follows (see bolded text for image location):

        <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) %>

The HTMLExtension method:

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();
    }

Everything works when run on the development server but when run with IIS, the image is not found. Generally I would use "Url.Content" to fix these problems but I can't place that in the Javascript. Does anyone has any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-10-16 18:30:46

您可能想将:更改

<%= 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) %> 

You might want to change:

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

to:

<%= Html.DatePicker("CodeValueNewEditModel_EffectStartDate", "CodeValueNewEditModel.EffectStartDate", Url.Content("~/Content/images/calendar.gif"),             Model.codeValueNewEditModel.EffectStartDate) %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文