开发者

Asp.net MVC2: CSS/Images paths on internal and local server

I have an Asp.net MVC2 web application. I built the applica开发者_JAVA技巧tion using VS2008 and tested on internal server, everything was perfect but when I deployed that web application on a local IIS the paths to images in web pages and in CSS file was not correct.

I need to change those paths for working with internal server and local server. How can I overcome this problem?


Have you tried opening the CSS and Images directly (i.e. not via being called on a page)? Does that work?

How do you link to them on your internal server, if the relative paths don't equate the same, you'll have the problem you describe. An example about how you might fix this is to use Url.Content().

e.g. Instead of using:

<link rel="Stylesheet" href="/Styles/Reset.css" />

You would use:

<link rel="Stylesheet" href="<%=Url.Content("~/Styles/Reset.css")%>" />

This would work out the URL depending upon where the application lives on that box - not relative for the web root.

Edit: In case you need to do this through C# code

// This can be used to create img tag
public static string Image(this HtmlHelper helper)
{
    UrlHelper urlHelper = ((Controller)helper.ViewContext.Controller).Url;
    TagBuilder imgTag = new TagBuilder("img");
    imgTag.MergeAttribute("src", urlHelper.Content("~/Content/images/image.gif"));
    imgTag.MergeAttribute("alt", "Alt text");

    return imgTag.ToString(TagRenderMode.SelfClosing);
}


To avoid this sort of problems you should always use the built-in helpers to generate URLs. For example:

<script type="text/javascript" src="<%= Url.Content("~/scripts/somescript.js") %>"></script>

and for links

<%= Html.ActionLink("Link text", "action", "controller") %>


Try something like this:

<link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />

The tilde ("~") represents the root and the Url.Content does the appropriate magic to get you a nice tidy reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜