ASP.NET MVC - Can't locate image file when run with IIS
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开发者_StackOverflow>
<%= 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?
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) %>
精彩评论