ASP.NET MVC - 使用 IIS 运行时无法找到图像文件
我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想将:更改
为:
You might want to change:
to: