1

I need to get the base path because currently my application is hosted as a sub-site like:

www.example.com/site1/site2/

Where my IIS application is setup in site2, but my razor pages and master page is referencing things like:

 <link href="/Assets/css/styles.css" rel="stylesheet">

When in this case it should be like:

 <link href="/site1/site2/Assets/css/styles.css" rel="stylesheet">

I also have images like:

 <img src="/Assets/images/logo.jpg" />

How can I get this to work in a dynamic way so I don't have to hard code the sub-directory somewhere?

2

1 Answer 1

5

You should use the built in helpers to get the relative path to your content.

 <link href="@Url.Content("~/Assets/css/styles.css")" rel="stylesheet">

For a site hosted at www.domain.com/site/subdirectory, the above will render out to

<link href="/site/subdirectory/Assets/css/styles.css" rel="stylesheet">

And, if you ever move your site or change your root path, you will not have to change your markup code.

More examples:

Images

<img src="@Url.Content("~/Assets/images/logo.jpg")" />

Javascript

<script type="text/javascript" src="@Url.Content("~/Assets/Scripts/jsfile.js")"></script>
Sign up to request clarification or add additional context in comments.

4 Comments

I don't know what you mean by link URLs?
This method is for providing the correct path to any static content on your web server. If you are asking about how to make links to controller/actions -> that is a different issue. But one we can help you with.
Yes for links, so the @Action.ActionLink have to be aware of the sub-directories etc.
@Html.ActionLink() references a controller/action and has nothing to do with subDirectories. When you put @Html.ActionLink("Text", "action", "controller") it has no bearing on what level of a subdirectory your application is at.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.